Skip to content

TranspilerError occurs when transpiling a circuit with if_test #14254

@YaegakiY

Description

@YaegakiY

Environment

  • Qiskit version: 2.0.0
  • Python version: Python 3.13.2
  • Operating system: Ubuntu 20.04.6 LTS

What is happening?

When transpiling a circuit that uses an if_test conditional statement, a TranspilerError occurs.

qiskit.transpiler.exceptions.TranspilerError: 'Unable to translate the operations in the circuit: ["if_else", "clifford"] to the backend\'s (or manually specified) target basis: {"h", "mcphase", "mcx_gray", "ecr", "diagonal", "mcsx", "mcu2", "save_superop", "csx", "mcy", "superop", "save_density_matrix", "u1", "mcu3", "ryy", "rzx", "set_statevector", "while_loop", "set_unitary", "continue_loop", "save_expval", "mcrz", "save_amplitudes", "cz", "qerror_loc", "cu3", "if_else", "save_probabilities", "cy", "crx", "save_amplitudes_sq", "rz", "mcz", "save_matrix_product_state", "mcu1", "mcry", "ccz", "sxdg", "p", "s", "roerror", "sdg", "save_expval_var", "set_superop", "save_statevector", "u2", "mcr", "ry", "store", "mcrx", "swap", "tdg", "multiplexer", "cx", "rxx", "quantum_channel", "kraus", "y", "u", "rzz", "z", "cu", "mcp", "switch_case", "cu2", "initialize", "save_probabilities_dict", "rx", "pauli", "reset", "save_stabilizer", "mcx", "id", "cp", "for_loop", "mcu", "ccx", "break_loop", "set_matrix_product_state", "save_statevector_dict", "save_unitary", "mcswap", "unitary", "measure", "set_density_matrix", "sx", "cswap", "t", "set_stabilizer", "u3", "save_state", "x", "cry", "cu1", "r", "delay", "save_clifford", "crz", "barrier", "snapshot"}. This likely means the target basis is not universal or there are additional equivalence rules needed in the EquivalenceLibrary being used. For more details on this error see: https://docs.quantum.ibm.com/api/qiskit/qiskit.transpiler.passes. BasisTranslator#translation-errors'

How can we reproduce the issue?

The following code triggers the issue:

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile
from qiskit_aer import Aer
from qiskit.transpiler.passes import *
from qiskit.transpiler import PassManager

qreg = QuantumRegister(7)
creg = ClassicalRegister(5)
cond_creg = ClassicalRegister(2)
qc = QuantumCircuit(qreg, creg, cond_creg)

qc.measure(qreg[4], creg[4])
with qc.if_test((creg[4], 0b1)) as else_1:
	qc.cx(2, 0)
	qc.iswap(4, 2)
with else_1:
	qc.cx(2, 0)

qc.measure(qreg[0], creg[0])
qc.measure(qreg[1], creg[1])
qc.measure(qreg[2], creg[2])
qc.measure(qreg[3], creg[3])
qc.measure(qreg[4], creg[4])


simulator = Aer.get_backend("aer_simulator")

p = PassManager(CollectCliffords())
qc = p.run(qc)

compiled_circuit = transpile(qc, backend = simulator, optimization_level = 3, routing_method = "sabre", layout_method = "noise_adaptive", approximation_degree = 1 )
job = simulator.run(compiled_circuit, shots=10000)
result = job.result().get_counts()
print(result)

What should happen?

The circuit should be successfully transpiled and executed.

Any suggestions?

Adding a dummy gate before the if_test block, such as composing an empty two-qubit gate (as shown below), allows the circuit to be transpiled and executed successfully.
Here is a minimal example that works:

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile
from qiskit_aer import Aer
from qiskit.transpiler.passes import *
from qiskit.transpiler import PassManager

qreg = QuantumRegister(7)
creg = ClassicalRegister(5)
cond_creg = ClassicalRegister(2)
qc = QuantumCircuit(qreg, creg, cond_creg)
#############################  Insert a dummy gate  #################################################
diffuser_gate = QuantumCircuit(QuantumRegister(2), name='Diffuser').to_gate(label='Diffuser')
grdc_qreg = QuantumRegister(2)
grdc_creg = ClassicalRegister(2)
grdc_qc = QuantumCircuit(grdc_qreg, grdc_creg)
grdc_qc.append(diffuser_gate, qargs=grdc_qreg)

qc.compose(grdc_qc, inplace = True, qubits = [5, 6])
#####################################################################################################

qc.measure(qreg[4], creg[4])
with qc.if_test((creg[4], 0b1)) as else_1:
	qc.cx(2, 0)
	qc.iswap(4, 2)
with else_1:
	qc.cx(2, 0)

qc.measure(qreg[0], creg[0])
qc.measure(qreg[1], creg[1])
qc.measure(qreg[2], creg[2])
qc.measure(qreg[3], creg[3])
qc.measure(qreg[4], creg[4])


simulator = Aer.get_backend("aer_simulator")

p = PassManager(CollectCliffords())
qc = p.run(qc)

compiled_circuit = transpile(qc, backend = simulator, optimization_level = 3, routing_method = "sabre", layout_method = "noise_adaptive", approximation_degree = 1 )
job = simulator.run(compiled_circuit, shots=10000)
result = job.result().get_counts()
print(result)

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