vector_to_operator throws Exception with qutip 4.5
See original GitHub issueHello, I noticed that vector_to_operator stopped working after updating to 4.5.
Simple code that gives error:
import qutip as qu
def zero_state_qutip(nb_qubits):
zero=qu.basis(2,0)
if nb_qubits>1:
for i in range(1,nb_qubits):
zero=qu.tensor(zero,qu.basis(2,0))
return zero
nb_qubits=4
Psi=zero_state_qutip(nb_qubits)
V=qu.vector_to_operator(Psi)
The code works fine with nb_qubits=2, but not with 4, 6 etc
The error is:
File “qutip\superoperator.py”, line 279, in vector_to_operator q.data = sp_reshape(op.data.T, shape[::-1]).T
File “qutip\sparse.py”, line 135, in sp_reshape return zcsr_reshape(A, shape[0], shape[1])
File “qutip/cy/spconvert.pyx”, line 254, in qutip.cy.spconvert.zcsr_reshape
Exception: Total size of array must be unchanged.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
QuTiP: Quantum Toolbox in Python (4.5)
QuTiP : Quantum Toolbox in Python (4.5)¶ · Manipulation and Creation of States and Operators · Functions acting on states and operators ·...
Read more >Functions — QuTiP 4.5 Documentation
qobj objects that are not passed via args will be passed on to the integrator in scipy which will raise an NotImplemented exception....
Read more >Change Log — QuTiP 4.7 Documentation
Change brmesolve to raise an exception when ode integration is not successful. ... The support for Numpy 1.20 that was first added in...
Read more >Quantum Toolbox in Python — QuTiP 4.7 Documentation
QuTiP : Quantum Toolbox in Python¶ · First things first · The quantum object class · Functions operating on Qobj class.
Read more >Quantum Information Processing — QuTiP 4.5 Documentation
QubitCircuit to represent a quantum circuit. Each quantum gate is saved as a class object qutip.qip.operations.Gate with information such as ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a note:
qutip.vector_to_operator
is part of the implementation of superoperators. The “vector” in the name refers to the idea that a superoperator in a Hilbert space can be represented as a vector on a different Hilbert space which comprises of two copies of the original space tensor-producted together. It doesn’t refer to a “vector” in the sense of a “ket”.The difference was introduced in c62a8e9, which is the amalgamated commit encompassing all the discussion in #1098.
qutip.vector_to_operator
expects to receive an argument whosedims
parameter is of the form[operator_dims , [1]]
, whereoperator_dims
is the dimensions of a regular operator (for example[[2], [2]]
for a single qubit Hilbert space, or[[2, 2], [2, 2]]
for an operator acting on a Hilbert space with two qubits in.The exception occurs because
vector_to_operator
attempts to access the two elements of theoperator_dims
array to discover the output shape. Since there is an insufficient number of lists for it to access, it just ends up reading the dimensions of the first two elements of the tensor product, andnp.prod
(used to calculate the output shape) doesn’t complain when passed scalars. Notice that running the code withnb_qubits = 1
will cause anIndexError
, as there are insufficient tensor-product elements.The immediate solution for your code is to use
qutip.ket2dm
, which converts a ket into a density matrix. I think this is what you’re actually trying to do.For a fix, there are two points:
qutip.super
, and not importing all the functions into the main namespace. This might help reduce user confusion going forwards, especially since most people aren’t using the superoperator formalism.Hi, thank you all for the answers, and sorry for the delay.
I understand there is some confusion because what I’m trying to do doesn’t really have a physical meaning, I simply wanted to convert between matrices and vectors because I’m looking for computational gains by doing so. If this was numpy, I would simply use numpy.reshape. I guess it actually makes sense if this is not a permitted operation in qutip, I was just exploiting the operator_to_vector function for a different reason.
@quantshah Thanks, I tried the code, but even the test_vec implementation fails with error File “qutip\superoperator.py”, line 278, in vector_to_operator shape = (np.prod(q.dims[0]), np.prod(q.dims[1]))
IndexError: list index out of range
@jakelishman Thanks for the tip for the tensor product basis state creation. It’s not a density matrix that I want to do, I just wanted to reshape the array.
As a side note, I notice that I have a very large computational cost when I work with dense matrices and state vectors, as you do when you simulate a quantum circuit with parametric rotations on each qubit, and this is due to the fact that qutip uses a sparse matrix representation. If I do the same operations with numpy I get a large computational gain, because the arrays are dense. Is there any way that I don’t know about to save the Qobj data in dense form?