Skip to content

Circuits optimised on transpile level >= 2 are not equivalent #10928

@lmondada

Description

@lmondada

Environment

  • Qiskit Terra version: 0.25.1
  • Python version: 3.10.13
  • Operating system: MacOS Ventura

What is happening?

When optimising the circuit below using optimization_level=2 or optimization_level=3 with target gate set basis_gates=['cx', 'u3'], the result is not equivalent to the input.

Input circuit

q_0: ─■───────────────■─
      │ ┌───┐   ┌───┐ │ 
q_1: ─■─┤ H ├─■─┤ H ├─■─
        └───┘ │ └───┘   
q_2: ─────────■─────────

Output circuit:

q_0: ─────────────────────────────────────
     ┌──────────────┐     ┌──────────────┐
q_1: ┤ U3(π/2,0,-π) ├──■──┤ U3(π/2,0,-π) ├
     ├─────────────┬┘┌─┴─┐├─────────────┬┘
q_2: ┤ U3(π/2,0,π) ├─┤ X ├┤ U3(π/2,0,π) ├─
     └─────────────┘ └───┘└─────────────┘ 

How can we reproduce the issue?

The following code outputs "Circuits are not equivalent".

import numpy as np
from qiskit import transpile, QuantumCircuit
import qiskit.quantum_info as qi


circ = QuantumCircuit(3)
circ.cz(0, 1)
circ.h(1)
circ.cz(1, 2)
circ.h(1)
circ.cz(0, 1)

# optimisation
opt_circ = transpile(circ, optimization_level=2, basis_gates=["cx", "u3"])

# check equivalence
def get_unitary(circ):
    op = qi.Operator(circ)
    return np.matrix(op.data)

u = get_unitary(circ)
u_opt = get_unitary(opt_circ)

u_uinv = u * u_opt.H
# fix global phase
u_uinv /= u_uinv[0, 0]

if np.isclose(u_uinv, np.eye(2**circ.num_qubits)).all():
    print("Circuits are equivalent")
else:
    print("Circuits are not equivalent")

What should happen?

The code should output Circuits are equivalent.

Any suggestions?

This only applies to gate sets including the u3 gate. Compiling to e.g. ['cx', 'rz', 'sx', 'x'] does not have this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions