Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.1k stars 2.34k forks source link

Potential incorrect qubit mapping leading to unexpected measurement results #13103

Open zywang12 opened 3 weeks ago

zywang12 commented 3 weeks ago

Environment

What is happening?

When running the following circuit, dcx is represented as a unitary matrix:

[[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
 [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
 [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
 [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]]

which applies to qubit 1 and qubit 0.

However, all backends are producing incorrect measurement results. It appears that the unitary is being applied to qubits 0 and 1 instead. Is there a potential error in the qubit mapping or in how the applied qubits are accessed?

How can we reproduce the issue?

from math import pi

from qiskit.circuit import QuantumCircuit
from qiskit import transpile
from qiskit_aer import AerSimulator, QasmSimulator
from qiskit_ibm_runtime.fake_provider import FakeBrisbane

qc = QuantumCircuit(2)
qc.x(0)
qc.barrier()
qc.rx(pi, 0)
qc.rx(-pi, 0)
qc.dcx(1, 0)
qc.measure_all()

backend1 = AerSimulator()
qc_after1 = transpile(qc, backend=backend1, optimization_level=2, translation_method='synthesis')
print("qc_after1:\n")
for inst in qc_after1:
    print(inst)
job1 = backend1.run(qc_after1)
print(job1.result().get_counts())
# {'10': 1024}

backend2 = FakeBrisbane()
job2 = backend2.run(qc_after1)
print(job2.result().get_counts())
# {'11': 30, '00': 27, '10': 967}
# FakeBrisbane is a noise backend, but we can also observe that '10' gets the most frequency.

What should happen?

The correct measurement result should be {'11': 1024}. For the noisy backends, '11' should have the highest frequency.

Any suggestions?

No response

jakelishman commented 3 weeks ago

Please can you read this documentation on Qiskit's conventions, and see if you still believe the problem exists: https://docs.quantum.ibm.com/api/qiskit/circuit#circuit-conventions.

jakelishman commented 3 weeks ago

I had a look, and I suspect the problem might be somewhere in Aer misinterpreting unitary instructions that aren't on qubits in the correct order. Using translation_method="synthesis" with Aer as the backend will cause everything to just get collapsed to unitary matrices. Omitting the measure_all and instead using qiskit.quantum_info.Statevector to simulate the circuit shows that it's finding a statevector of [0, 0, 0, 1.0], which is consistent with measuring 11. So it might be Aer making a mistake when applying a unitary gate to [1, 0] (but I'm not sure).

zywang12 commented 3 weeks ago

Thank you for your prompt reply! Should I open the same issue in qiskit-aer and close this one?