From 528740f1a55305f4efa2a70052d8368fb0737352 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Mon, 4 Apr 2022 14:51:54 +0100 Subject: [PATCH] Add regression tests of single-bit conditions in QuantumCircuit.compose (#7806) 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> --- test/python/circuit/test_compose.py | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/python/circuit/test_compose.py b/test/python/circuit/test_compose.py index d5b984c22fb5..f8b72a87018d 100644 --- a/test/python/circuit/test_compose.py +++ b/test/python/circuit/test_compose.py @@ -21,6 +21,7 @@ from qiskit.circuit import ( QuantumRegister, ClassicalRegister, + Clbit, QuantumCircuit, Parameter, Gate, @@ -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()