Closed pranavm1502 closed 2 years ago
Thanks for the report!
This particular case is the exact same behaviour that causes #7341, where discussion seems to have stalled a little bit. The trick is that the synthesis routines in the transpiler have some approximations turned on by default (whether or not they should be doing that is another question...). You can work around the issue by setting approximation_degree=0
in the call to transpile
.
For others, a more minimal reproducer of the error:
import qiskit
from qiskit.test.mock import FakeLagos
import numpy as np
backend = FakeLagos()
circuit = qiskit.QuantumCircuit(2)
for i in range(4):
circuit.rz(3*np.pi/4, 1)
circuit.sx(1)
circuit.cnot(0,1)
approximate = qiskit.transpile(circuit, backend=backend, optimization_level=3, approximation_degree=None)
exact = qiskit.transpile(circuit, backend=backend, optimization_level=3, approximation_degree=0)
def reduce_circuit(circuit):
"""Quick hack to reduce a larger circuit to the minimal set of qubits."""
active_qubits = set()
for _, qargs, _ in circuit.data:
active_qubits.update(qargs)
bit_map = {bit: i for i, bit in enumerate(active_qubits)}
out = qiskit.QuantumCircuit(len(bit_map))
out.global_phase = circuit.global_phase
for op, qargs, _ in circuit.data:
out.append(op, [bit_map[q] for q in qargs], [])
return out
approximate = reduce_circuit(approximate)
exact = reduce_circuit(exact)
You can then see that (using qiskit.quantum_info.Operator
to get the matrix form of the circuits) that Operator(circuit)
is equal to Operator(exact)
, but not to Operator(approximate)
.
Thanks Jake! Setting the approximation_degree=0
indeed solves the problem.
I would suggest that by default it should always be set to zero until the issue is resolved. Incorrect transpilation on a 5 CNOT 2Q circuit sounds like a bad default setting.
Since this is already tacked by https://github.com/Qiskit/qiskit-terra/issues/8043, closing. Thanks @pranavm1502 !
Informations
Qiskit version: {'qiskit-terra': '0.19.2', 'qiskit-aer': '0.10.3', 'qiskit-ignis': '0.7.0', 'qiskit-ibmq-provider': '0.18.3', 'qiskit-aqua': None, 'qiskit': '0.34.2', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None}
Python version: Python 3.10
Operating system: Mac OS Monterey 12.3.1
What is the current behavior?
Transpiled 2qubit circuit with optimization level 3 is not equivalent to the original circuit
Steps to reproduce the problem
Look at the attached jupyter notebook Qiskit transpiler level 3 investigation.ipynb.zip
What is the expected behavior?
The result outputs should be same (upto shot noise).
Suggested solutions