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

Transpiled circuit with optimization level 3 is not equivalent to the original circuit #7961

Closed
pranavm1502 opened this issue Apr 18, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@pranavm1502
Copy link

pranavm1502 commented Apr 18, 2022

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

@pranavm1502 pranavm1502 added the bug Something isn't working label Apr 18, 2022
@pranavm1502 pranavm1502 changed the title + Transpiled circuit with optimization level 3 is not equivalent to the original circuit Apr 18, 2022
@pranavm1502 pranavm1502 reopened this Apr 20, 2022
@jakelishman jakelishman transferred this issue from Qiskit/qiskit-metapackage Apr 20, 2022
@jakelishman
Copy link
Member

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).

@pranavm1502
Copy link
Author

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.

@1ucian0
Copy link
Member

1ucian0 commented May 13, 2022

Since this is already tacked by #8043, closing. Thanks @pranavm1502 !

@1ucian0 1ucian0 closed this as completed May 13, 2022
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

No branches or pull requests

3 participants