|
| 1 | +# This code is part of Qiskit. |
| 2 | +# |
| 3 | +# (C) Copyright IBM 2025 |
| 4 | +# |
| 5 | +# This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +# obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +# |
| 9 | +# Any modifications or derivative works of this code must retain this |
| 10 | +# copyright notice, and modified files need to carry a notice indicating |
| 11 | +# that they have been altered from the originals. |
| 12 | + |
| 13 | +# pylint: disable=missing-class-docstring,missing-module-docstring,missing-function-docstring |
| 14 | + |
| 15 | +from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister |
| 16 | +from qiskit.circuit.classical import expr, types |
| 17 | +from qiskit.transpiler.passes import ContractIdleWiresInControlFlow |
| 18 | + |
| 19 | +from test import QiskitTestCase # pylint: disable=wrong-import-order |
| 20 | + |
| 21 | + |
| 22 | +class TestContractIdleWiresInControlFlow(QiskitTestCase): |
| 23 | + def test_simple_body(self): |
| 24 | + qc = QuantumCircuit(3, 1) |
| 25 | + with qc.while_loop((qc.clbits[0], False)): |
| 26 | + qc.cx(0, 1) |
| 27 | + qc.noop(2) |
| 28 | + |
| 29 | + expected = QuantumCircuit(3, 1) |
| 30 | + with expected.while_loop((expected.clbits[0], False)): |
| 31 | + expected.cx(0, 1) |
| 32 | + |
| 33 | + self.assertEqual(ContractIdleWiresInControlFlow()(qc), expected) |
| 34 | + |
| 35 | + def test_nothing_to_do(self): |
| 36 | + qc = QuantumCircuit(3, 1) |
| 37 | + with qc.for_loop(range(3)): |
| 38 | + qc.h(0) |
| 39 | + qc.cx(0, 1) |
| 40 | + self.assertEqual(ContractIdleWiresInControlFlow()(qc), qc) |
| 41 | + |
| 42 | + def test_disparate_if_else_left_alone(self): |
| 43 | + qc = QuantumCircuit(3, 1) |
| 44 | + # The true body only uses 0, the false body only uses (1, 2), but because they're part of |
| 45 | + # the shared op, there is no valid contraction here. |
| 46 | + with qc.if_test((qc.clbits[0], True)) as else_: |
| 47 | + qc.h(0) |
| 48 | + with else_: |
| 49 | + qc.cx(1, 2) |
| 50 | + self.assertEqual(ContractIdleWiresInControlFlow()(qc), qc) |
| 51 | + |
| 52 | + def test_contract_if_else_both_bodies(self): |
| 53 | + qc = QuantumCircuit(3, 1) |
| 54 | + # Explicit idle in the true body only. |
| 55 | + with qc.if_test((qc.clbits[0], True)) as else_: |
| 56 | + qc.h(0) |
| 57 | + qc.cx(0, 2) |
| 58 | + qc.noop(1) |
| 59 | + with else_: |
| 60 | + qc.cz(0, 2) |
| 61 | + # Explicit idle in the false body only. |
| 62 | + with qc.if_test((qc.clbits[0], True)) as else_: |
| 63 | + qc.h(0) |
| 64 | + qc.cx(0, 1) |
| 65 | + with else_: |
| 66 | + qc.cz(0, 1) |
| 67 | + qc.noop(2) |
| 68 | + # Explicit idle in both bodies. |
| 69 | + with qc.if_test((qc.clbits[0], True)) as else_: |
| 70 | + qc.h(1) |
| 71 | + qc.cx(1, 2) |
| 72 | + qc.noop(0) |
| 73 | + with else_: |
| 74 | + qc.cz(1, 2) |
| 75 | + qc.noop(0) |
| 76 | + |
| 77 | + expected = QuantumCircuit(3, 1) |
| 78 | + with expected.if_test((expected.clbits[0], True)) as else_: |
| 79 | + expected.h(0) |
| 80 | + expected.cx(0, 2) |
| 81 | + with else_: |
| 82 | + expected.cz(0, 2) |
| 83 | + with expected.if_test((expected.clbits[0], True)) as else_: |
| 84 | + expected.h(0) |
| 85 | + expected.cx(0, 1) |
| 86 | + with else_: |
| 87 | + expected.cz(0, 1) |
| 88 | + with expected.if_test((expected.clbits[0], True)) as else_: |
| 89 | + expected.h(1) |
| 90 | + expected.cx(1, 2) |
| 91 | + with else_: |
| 92 | + expected.cz(1, 2) |
| 93 | + |
| 94 | + self.assertEqual(ContractIdleWiresInControlFlow()(qc), expected) |
| 95 | + |
| 96 | + def test_recursively_contract(self): |
| 97 | + qc = QuantumCircuit(3, 1) |
| 98 | + with qc.if_test((qc.clbits[0], True)): |
| 99 | + qc.h(0) |
| 100 | + with qc.if_test((qc.clbits[0], True)): |
| 101 | + qc.cx(0, 1) |
| 102 | + qc.noop(2) |
| 103 | + with qc.while_loop((qc.clbits[0], True)): |
| 104 | + with qc.if_test((qc.clbits[0], True)) as else_: |
| 105 | + qc.h(0) |
| 106 | + qc.noop(1, 2) |
| 107 | + with else_: |
| 108 | + qc.cx(0, 1) |
| 109 | + qc.noop(2) |
| 110 | + |
| 111 | + expected = QuantumCircuit(3, 1) |
| 112 | + with expected.if_test((expected.clbits[0], True)): |
| 113 | + expected.h(0) |
| 114 | + with expected.if_test((expected.clbits[0], True)): |
| 115 | + expected.cx(0, 1) |
| 116 | + with expected.while_loop((expected.clbits[0], True)): |
| 117 | + with expected.if_test((expected.clbits[0], True)) as else_: |
| 118 | + expected.h(0) |
| 119 | + with else_: |
| 120 | + expected.cx(0, 1) |
| 121 | + |
| 122 | + actual = ContractIdleWiresInControlFlow()(qc) |
| 123 | + self.assertNotEqual(qc, actual) # Smoke test. |
| 124 | + self.assertEqual(actual, expected) |
| 125 | + |
| 126 | + def test_handles_vars_in_contraction(self): |
| 127 | + a = expr.Var.new("a", types.Bool()) |
| 128 | + b = expr.Var.new("b", types.Uint(8)) |
| 129 | + c = expr.Var.new("c", types.Bool()) |
| 130 | + |
| 131 | + qc = QuantumCircuit(3, inputs=[a]) |
| 132 | + qc.add_var(b, 5) |
| 133 | + with qc.if_test(a): |
| 134 | + qc.add_var(c, False) |
| 135 | + with qc.if_test(c): |
| 136 | + qc.x(0) |
| 137 | + qc.noop(1, 2) |
| 138 | + with qc.switch(b) as case: |
| 139 | + with case(0): |
| 140 | + qc.x(0) |
| 141 | + with case(1): |
| 142 | + qc.noop(0, 1) |
| 143 | + with case(case.DEFAULT): |
| 144 | + with qc.if_test(a): |
| 145 | + qc.x(0) |
| 146 | + qc.noop(1, 2) |
| 147 | + |
| 148 | + expected = QuantumCircuit(3, inputs=[a]) |
| 149 | + expected.add_var(b, 5) |
| 150 | + with expected.if_test(a): |
| 151 | + expected.add_var(c, False) |
| 152 | + with expected.if_test(c): |
| 153 | + expected.x(0) |
| 154 | + with expected.switch(b) as case: |
| 155 | + with case(0): |
| 156 | + expected.x(0) |
| 157 | + with case(1): |
| 158 | + pass |
| 159 | + with case(case.DEFAULT): |
| 160 | + with expected.if_test(a): |
| 161 | + expected.x(0) |
| 162 | + |
| 163 | + actual = ContractIdleWiresInControlFlow()(qc) |
| 164 | + self.assertNotEqual(qc, actual) # Smoke test. |
| 165 | + self.assertEqual(actual, expected) |
| 166 | + |
| 167 | + def test_handles_registers_in_contraction(self): |
| 168 | + qr = QuantumRegister(3, "q") |
| 169 | + cr1 = ClassicalRegister(3, "cr1") |
| 170 | + cr2 = ClassicalRegister(3, "cr2") |
| 171 | + |
| 172 | + qc = QuantumCircuit(qr, cr1, cr2) |
| 173 | + with qc.if_test((cr1, 3)): |
| 174 | + with qc.if_test((cr2, 3)): |
| 175 | + qc.noop(0, 1, 2) |
| 176 | + expected = QuantumCircuit(qr, cr1, cr2) |
| 177 | + with expected.if_test((cr1, 3)): |
| 178 | + with expected.if_test((cr2, 3)): |
| 179 | + pass |
| 180 | + |
| 181 | + actual = ContractIdleWiresInControlFlow()(qc) |
| 182 | + self.assertNotEqual(qc, actual) # Smoke test. |
| 183 | + self.assertEqual(actual, expected) |
0 commit comments