Skip to content
New issue

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

Partial trace operation throws up error once it is broken #7612

Closed
zohimchandani opened this issue Feb 2, 2022 · 2 comments · Fixed by #7613
Closed

Partial trace operation throws up error once it is broken #7612

zohimchandani opened this issue Feb 2, 2022 · 2 comments · Fixed by #7613
Assignees
Labels
bug Something isn't working

Comments

@zohimchandani
Copy link

Informations

  • Qiskit Aer version: 0.10.2
  • Python version: 3.6.9
  • Operating system: Mac OS

What is the current behavior?

See minimal code example below

Steps to reproduce the problem


# 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?

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

@zohimchandani zohimchandani added the bug Something isn't working label Feb 2, 2022
@jakelishman jakelishman transferred this issue from Qiskit/qiskit-aer Feb 2, 2022
@jakelishman
Copy link
Member

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:

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.

@jakelishman jakelishman self-assigned this Feb 2, 2022
@jakelishman
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants