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

Operator(circuit) does not return the correct unitary #2622

Closed
rabaniten opened this issue Jun 12, 2019 · 3 comments · Fixed by #2723
Closed

Operator(circuit) does not return the correct unitary #2622

rabaniten opened this issue Jun 12, 2019 · 3 comments · Fixed by #2723
Assignees
Labels
bug Something isn't working
Milestone

Comments

@rabaniten
Copy link
Contributor

Information

-Qiskit Terra version: 0.8.1
-Python version: 3.7.2
-Operating system: MacOS Mojave

What is the current behavior?

The data stored in Operator(circuit).data (with Operator imported from qiskit.quantum_info
) seems not to correspond to the right unitary for certain circuits.

Steps to reproduce the problem

Choose the following circuit:

qasm = 'OPENQASM 2.0;'
qasm += 'include "qelib1.inc";'
qasm += 'qreg q[3];'
qasm += 'creg c[3];'
qasm += 'rx(3.141592653589793) q[2];'
qasm += 'ry(0.294524311274043) q[2];'
qasm += 'cx q[0],q[2];'
qasm += 'ry(-0.0981747704246810) q[2];'
qasm += 'cx q[1],q[2];'
qasm += 'ry(0.0981747704246810) q[2];'
qasm += 'cx q[0],q[2];'
qasm += 'ry(-3.141592653589793) q[2];'
circuit = QuantumCircuit.from_qasm_str(qasm)

One finds a different result [than the one found by(Operator(circuit).data] using the unitary_simulator:

simulator = BasicAer.get_backend('unitary_simulator')
result = execute(circuit, simulator).result()
unitary = result.get_unitary(circuit)
matrix_equal(Operator(circuit).data, unitary, ignore_phase=True)

The last line returns False.

@nonhermitian
Copy link
Contributor

This does indeed look like a bug in the operator class. The numerical values appear to be correct, but the indices are not. So it seems that there is some array manipulations that are not correct.

@nonhermitian
Copy link
Contributor

To make reproducing easier, here is the circuit:

q = QuantumRegister(3, 'q')
c = ClassicalRegister(3, 'c')
qc = QuantumCircuit(q, c)
qc.rx(3.141592653589793, q[2])
qc.ry(0.294524311274043, q[2])
qc.cx(q[0], q[2])
qc.ry(-0.0981747704246810, q[2])
qc.cx(q[1], q[2])
qc.ry(0.0981747704246810, q[2])
qc.cx(q[0], q[2])
qc.ry(-3.141592653589793, q[2])

@nonhermitian nonhermitian added the bug Something isn't working label Jun 13, 2019
@ajavadia ajavadia added this to the 0.9 milestone Jun 15, 2019
@chriseclectic
Copy link
Member

Looks like the single qubit gate is getting applied to the wrong index, but only for rx, ry, rz.
If I run the following circuit with some debugging prints added the last three are getting applied to q[0] instead of q[2].

q = QuantumRegister(3, 'q')
qc = QuantumCircuit(q)
qc.iden(q[2])
qc.x(q[2])
qc.y(q[2])
qc.z(q[2])
qc.h(q[2])
qc.s(q[2])
qc.sdg(q[2])
qc.t(q[2])
qc.tdg(q[2])
qc.u1(0.2, q[2])
qc.u2(0.1, 0.2, q[2])
qc.u3(0.1, 0.2, 0.3, q[2])
qc.rz(0.294524311274043, q[2])
qc.ry(0.294524311274043, q[2])
qc.rz(0.294524311274043, q[2])

My guess is there is a bug in how Operator unrolls gates without a matrix def. I'll look into a fix.

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.

4 participants