Skip to content

Commit

Permalink
Do IfElseOp test without builder interface
Browse files Browse the repository at this point in the history
Before, we used the builder interface for an ifelse op. Some details of
the circuit built, in particular the mapping of wires is not deterministic.
We could have used canonicalize_control_flow. But instead we construct the
IfElseOp manually. This removes the complexity of the builder interface from
this test.
  • Loading branch information
jlapeyre committed Jul 10, 2023
1 parent c32d69c commit d4bc8bd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions test/python/transpiler/test_consolidate_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

import unittest
import numpy as np
import copy

from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.circuit import QuantumCircuit, QuantumRegister, IfElseOp
from qiskit.circuit.library import U2Gate, SwapGate, CXGate
from qiskit.extensions import UnitaryGate
from qiskit.converters import circuit_to_dag
Expand Down Expand Up @@ -430,7 +431,7 @@ def test_identity_1q_unitary_is_removed(self):

def test_descent_into_control_flow(self):
"""Test consolidation in blocks of control flow op is the same as at top level."""
qc = QuantumCircuit(2)
qc = QuantumCircuit(2, 1)
u2gate1 = U2Gate(-1.2, np.pi)
u2gate2 = U2Gate(-3.4, np.pi)
qc.append(u2gate1, [0])
Expand All @@ -444,11 +445,16 @@ def test_descent_into_control_flow(self):
result_top = pass_manager.run(qc)

qc_control_flow = QuantumCircuit(2, 1)
with qc_control_flow.if_test((0, False)):
qc_control_flow.append(u2gate1, [0])
qc_control_flow.append(u2gate2, [1])
qc_control_flow.cx(0, 1)
qc_control_flow.cx(1, 0)
qc_block = copy.deepcopy(qc)

qc_block = QuantumCircuit(qc.qubits, qc.clbits)
qc_block.append(u2gate1, [0])
qc_block.append(u2gate2, [1])
qc_block.cx(0, 1)
qc_block.cx(1, 0)

ifop = IfElseOp((qc.clbits[0], False), qc_block, None)
qc_control_flow.append(ifop, qc.qubits, qc.clbits)

pass_manager = PassManager()
pass_manager.append(Collect2qBlocks())
Expand Down

0 comments on commit d4bc8bd

Please sign in to comment.