We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
See minimal code example below
# Import Qiskit from qiskit import QuantumCircuit import qiskit.quantum_info as qi from qiskit import Aer, execute simulator = Aer.get_backend('statevector_simulator', seed = 100) qc_AB = QuantumCircuit(2) qc_AB.h(0) qc_AB.cx(0,1) sv = execute(qc_AB, simulator).result().get_statevector() rho_B = qi.partial_trace(sv,[0]) #trace out qubit 0
Now If I provide an empty list to trace out then it throws up an error which is expected
rho_B = qi.partial_trace(sv, []) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-26-a145dac87eb0> in <module> ----> 1 rho_B = qi.partial_trace(sv,[]) ~/PQKs/pqks/lib/python3.6/site-packages/qiskit/quantum_info/states/utils.py in partial_trace(state, qargs) 56 if isinstance(state, Statevector): 57 trace_systems = len(state._op_shape.dims_l()) - 1 - np.array(qargs) ---> 58 arr = state._data.reshape(state._op_shape.tensor_shape) 59 rho = np.tensordot(arr, arr.conj(), axes=(trace_systems, trace_systems)) 60 rho = np.reshape(rho, traced_shape.shape) ValueError: cannot reshape array of size 4 into shape (2,2,2,2)
After the error, if I now recall a previous instruction that successfully ran then the error reappears.
rho_B = qi.partial_trace(sv,[0]) #trace out qubit 0 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-27-8cbae0b90ecc> in <module> ----> 1 rho_B = qi.partial_trace(sv,[0]) ~/PQKs/pqks/lib/python3.6/site-packages/qiskit/quantum_info/states/utils.py in partial_trace(state, qargs) 56 if isinstance(state, Statevector): 57 trace_systems = len(state._op_shape.dims_l()) - 1 - np.array(qargs) ---> 58 arr = state._data.reshape(state._op_shape.tensor_shape) 59 rho = np.tensordot(arr, arr.conj(), axes=(trace_systems, trace_systems)) 60 rho = np.reshape(rho, traced_shape.shape) ValueError: cannot reshape array of size 4 into shape (2,2,2,2)
It seems as if the qi.partial_trace operation is in some state that once it is broken, it cant function again?
qi.partial_trace
If I use .get_statevector().data the above issue disappears.
.get_statevector().data
Operation should not be state-bound and work regardless?
The text was updated successfully, but these errors were encountered:
I've transferred this to Terra, because the issue isn't related to Aer, it's just about qiskit.quantum_info, which is in Terra:
qiskit.quantum_info
In [1]: from qiskit.quantum_info import Statevector, partial_trace ...: sv = Statevector([0, np.sqrt(0.5), np.sqrt(0.5), 0]) ...: try: ...: partial_trace(sv, []) ...: except ValueError: ...: pass ...: partial_trace(sv, [0])
gives
ValueError Traceback (most recent call last) Input In [1], in <module> 5 except ValueError: 6 pass ----> 7 partial_trace(sv, [0]) File ~/code/qiskit/terra/qiskit/quantum_info/states/utils.py:57, in partial_trace(state, qargs) 55 if isinstance(state, Statevector): 56 trace_systems = len(state._op_shape.dims_l()) - 1 - np.array(qargs) ---> 57 arr = state._data.reshape(state._op_shape.tensor_shape) 58 rho = np.tensordot(arr, arr.conj(), axes=(trace_systems, trace_systems)) 59 rho = np.reshape(rho, traced_shape.shape) ValueError: cannot reshape array of size 4 into shape (2,2,2,2)
I'm guessing that somewhere along the line, Statevector._op_shape gets mutated in place, and never fixed because of the exception.
Statevector._op_shape
Sorry, something went wrong.
Also, I'm not super convinced that passing an empty list should be an error - I'd have thought we should just return the density matrix of the state.
partial_trace
jakelishman
Successfully merging a pull request may close this issue.
Informations
What is the current behavior?
See minimal code example below
Steps to reproduce the problem
Now If I provide an empty list to trace out then it throws up an error which is expected
After the error, if I now recall a previous instruction that successfully ran then the error reappears.
It seems as if the
qi.partial_trace
operation is in some state that once it is broken, it cant function again?If I use
.get_statevector().data
the above issue disappears.What is the expected behavior?
Operation should not be state-bound and work regardless?
Suggested solutions
The text was updated successfully, but these errors were encountered: