Skip to content

Commit

Permalink
Add regression tests of single-bit conditions in QuantumCircuit.compo…
Browse files Browse the repository at this point in the history
…se (#7806) (#7878)

Recent work in #7653 had introduced a bug where a private method
`DAGCircuit._map_condition` would fail on single-bit conditionals.  This
method is used by `QuantumCircuit.compose`, and our CI should have
caught the issue before merge.  This commit introduces regression tests
to prevent this from happening again.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 528740f)

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
mergify[bot] and jakelishman authored Apr 4, 2022
1 parent 41f9774 commit 080950b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/python/circuit/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qiskit.circuit import (
QuantumRegister,
ClassicalRegister,
Clbit,
QuantumCircuit,
Parameter,
Gate,
Expand Down Expand Up @@ -592,6 +593,64 @@ def test_wrapping_unitary_circuit(self):
qc = qc_init.compose(qc_nonunitary, wrap=True)
self.assertIsInstance(qc.data[1][0], Instruction)

def test_single_bit_condition(self):
"""Test that compose can correctly handle circuits that contain conditions on single
bits. This is a regression test of the bug that broke qiskit-experiments in gh-7653."""
base = QuantumCircuit(1, 1)
base.x(0).c_if(0, True)
test = QuantumCircuit(1, 1).compose(base)
self.assertIsNot(base.clbits[0], test.clbits[0])
self.assertEqual(base, test)
self.assertIs(test.data[0][0].condition[0], test.clbits[0])

def test_condition_mapping_ifelseop(self):
"""Test that the condition in an `IfElseOp` is correctly mapped to a new set of bits and
registers."""
base_loose = Clbit()
base_creg = ClassicalRegister(2)
base_qreg = QuantumRegister(1)
base = QuantumCircuit(base_qreg, [base_loose], base_creg)
with base.if_test((base_loose, True)):
base.x(0)
with base.if_test((base_creg, 3)):
base.x(0)

test_loose = Clbit()
test_creg = ClassicalRegister(2)
test_qreg = QuantumRegister(1)
test = QuantumCircuit(test_qreg, [test_loose], test_creg).compose(base)

bit_instruction, *_ = test.data[0]
reg_instruction, *_ = test.data[1]
self.assertIs(bit_instruction.condition[0], test_loose)
self.assertEqual(bit_instruction.condition, (test_loose, True))
self.assertIs(reg_instruction.condition[0], test_creg)
self.assertEqual(reg_instruction.condition, (test_creg, 3))

def test_condition_mapping_whileloopop(self):
"""Test that the condition in a `WhileLoopOp` is correctly mapped to a new set of bits and
registers."""
base_loose = Clbit()
base_creg = ClassicalRegister(2)
base_qreg = QuantumRegister(1)
base = QuantumCircuit(base_qreg, [base_loose], base_creg)
with base.while_loop((base_loose, True)):
base.x(0)
with base.while_loop((base_creg, 3)):
base.x(0)

test_loose = Clbit()
test_creg = ClassicalRegister(2)
test_qreg = QuantumRegister(1)
test = QuantumCircuit(test_qreg, [test_loose], test_creg).compose(base)

bit_instruction, *_ = test.data[0]
reg_instruction, *_ = test.data[1]
self.assertIs(bit_instruction.condition[0], test_loose)
self.assertEqual(bit_instruction.condition, (test_loose, True))
self.assertIs(reg_instruction.condition[0], test_creg)
self.assertEqual(reg_instruction.condition, (test_creg, 3))


if __name__ == "__main__":
unittest.main()

0 comments on commit 080950b

Please sign in to comment.