From 9259102eefbf30229a94796efd616ec48829f750 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 25 Sep 2024 11:52:30 -0400 Subject: [PATCH 1/9] Deprecate the condition attribute and related functionality This commit deprecates the Instruction.condition and c_if() method for removal in 2.0. This functionality has been superseded by the more sophisiticated `IfElseOp` for some time now. Removing the condition property will simplify the Qiskit data model as it is implemented in a kind of ad-hoc manner that adds additional overhead to manually check it in many places. For the unittest modifications the deprecation warning for the .condtion getter is suppressed for the entire suite because this gets triggered internally by the transpiler and a lot of other places, including from rust code as until it is removed we need to use it to check that piece of the data model. Fixes #9556 --- qiskit/circuit/barrier.py | 2 + qiskit/circuit/delay.py | 2 + qiskit/circuit/instruction.py | 2 + qiskit/circuit/instructionset.py | 2 + qiskit/circuit/singleton.py | 2 + qiskit/circuit/store.py | 2 + qiskit/qasm2/parse.py | 17 +- qiskit/qpy/binary_io/circuits.py | 13 + .../utils/convert_conditions_to_if_ops.py | 5 + ...ecate-condition_c_if-9548e5522814fe9c.yaml | 38 + .../classical/test_expr_constructors.py | 12 +- .../circuit/library/test_permutation.py | 3 +- test/python/circuit/library/test_qft.py | 3 +- .../circuit/test_circuit_load_from_qpy.py | 40 +- .../python/circuit/test_circuit_operations.py | 51 +- .../python/circuit/test_circuit_properties.py | 96 ++- test/python/circuit/test_circuit_qasm.py | 12 +- .../circuit/test_commutation_checker.py | 45 +- test/python/circuit/test_compose.py | 24 +- test/python/circuit/test_control_flow.py | 15 +- .../circuit/test_control_flow_builders.py | 652 ++++++++++++------ .../circuit/test_extensions_standard.py | 9 +- .../python/circuit/test_instruction_repeat.py | 9 +- test/python/circuit/test_instructions.py | 66 +- test/python/circuit/test_random_circuit.py | 9 +- .../circuit/test_registerless_circuit.py | 48 +- test/python/circuit/test_singleton.py | 63 +- test/python/circuit/test_store.py | 6 +- test/python/compiler/test_assembler.py | 12 +- test/python/compiler/test_disassembler.py | 30 +- test/python/compiler/test_transpiler.py | 33 +- test/python/converters/test_circuit_to_dag.py | 3 +- .../test_circuit_to_dagdependency.py | 6 +- .../test_circuit_to_dagdependency_v2.py | 3 +- .../converters/test_circuit_to_instruction.py | 18 +- .../converters/test_dag_to_dagdependency.py | 6 +- .../test_dag_to_dagdependency_v2.py | 3 +- test/python/dagcircuit/test_collect_blocks.py | 77 ++- test/python/dagcircuit/test_compose.py | 30 +- test/python/dagcircuit/test_dagcircuit.py | 104 ++- .../primitives/test_backend_sampler_v2.py | 12 +- test/python/primitives/test_primitive.py | 3 +- .../primitives/test_statevector_sampler.py | 9 +- .../basic_provider/test_basic_simulator.py | 15 +- test/python/qasm2/test_arxiv_examples.py | 84 ++- test/python/qasm2/test_circuit_methods.py | 6 +- test/python/qasm2/test_export.py | 12 +- test/python/qasm2/test_structure.py | 87 ++- test/python/qasm3/test_export.py | 33 +- .../operators/symplectic/test_clifford.py | 3 +- .../test_instruction_alignments.py | 6 +- .../legacy_scheduling/test_scheduling_pass.py | 99 ++- .../test_barrier_before_final_measurements.py | 24 +- .../transpiler/test_basis_translator.py | 24 +- test/python/transpiler/test_check_map.py | 3 +- .../python/transpiler/test_clifford_passes.py | 3 +- .../transpiler/test_collect_2q_blocks.py | 12 +- .../transpiler/test_collect_multiq_blocks.py | 15 +- .../test_commutative_cancellation.py | 9 +- .../test_commutative_inverse_cancellation.py | 3 +- .../transpiler/test_consolidate_blocks.py | 3 +- .../test_convert_conditions_to_if_ops.py | 50 +- test/python/transpiler/test_decompose.py | 12 +- .../transpiler/test_elide_permutations.py | 3 +- test/python/transpiler/test_gate_direction.py | 24 +- .../transpiler/test_high_level_synthesis.py | 18 +- .../test_linear_functions_passes.py | 3 +- .../test_optimize_1q_decomposition.py | 10 +- .../transpiler/test_optimize_1q_gates.py | 24 +- .../test_optimize_swap_before_measure.py | 6 +- .../transpiler/test_preset_passmanagers.py | 6 +- ...st_remove_diagonal_gates_before_measure.py | 3 +- .../test_remove_final_measurements.py | 12 +- ...test_reset_after_measure_simplification.py | 44 +- test/python/transpiler/test_sabre_swap.py | 51 +- .../test_scheduling_padding_pass.py | 99 ++- .../transpiler/test_unroll_3q_or_more.py | 3 +- .../python/transpiler/test_unroll_forloops.py | 3 +- .../visualization/test_circuit_drawer.py | 6 +- .../visualization/test_circuit_text_drawer.py | 248 ++++--- test/python/visualization/test_dag_drawer.py | 3 +- test/python/visualization/test_utils.py | 8 +- test/utils/base.py | 7 + 83 files changed, 1799 insertions(+), 882 deletions(-) create mode 100644 releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml diff --git a/qiskit/circuit/barrier.py b/qiskit/circuit/barrier.py index c339066b4dce..d04769b68d39 100644 --- a/qiskit/circuit/barrier.py +++ b/qiskit/circuit/barrier.py @@ -19,6 +19,7 @@ from __future__ import annotations from qiskit.exceptions import QiskitError +from qiskit.utils import deprecate_func from .instruction import Instruction @@ -44,5 +45,6 @@ def inverse(self, annotated: bool = False): """Special case. Return self.""" return Barrier(self.num_qubits) + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0") def c_if(self, classical, val): raise QiskitError("Barriers are compiler directives and cannot be conditional.") diff --git a/qiskit/circuit/delay.py b/qiskit/circuit/delay.py index 25e7a6f3356c..157ae4c39069 100644 --- a/qiskit/circuit/delay.py +++ b/qiskit/circuit/delay.py @@ -19,6 +19,7 @@ from qiskit.circuit.gate import Gate from qiskit.circuit import _utils from qiskit.circuit.parameterexpression import ParameterExpression +from qiskit.utils import deprecate_func @_utils.with_gate_array(np.eye(2, dtype=complex)) @@ -42,6 +43,7 @@ def inverse(self, annotated: bool = False): """Special case. Return self.""" return self + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0") def c_if(self, classical, val): raise CircuitError("Conditional Delay is not yet implemented.") diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py index 557d7df21ee1..100a1f31a0a9 100644 --- a/qiskit/circuit/instruction.py +++ b/qiskit/circuit/instruction.py @@ -160,6 +160,7 @@ def to_mutable(self): return self.copy() @property + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0", is_property=True, stacklevel=2) def condition(self): """The classical condition on the instruction.""" return self._condition @@ -499,6 +500,7 @@ def inverse(self, annotated: bool = False): inverse_gate.definition = inverse_definition return inverse_gate + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0") def c_if(self, classical, val): """Set a classical equality condition on this instruction between the register or cbit ``classical`` and value ``val``. diff --git a/qiskit/circuit/instructionset.py b/qiskit/circuit/instructionset.py index cc8a050fd2b0..a3f06a90040c 100644 --- a/qiskit/circuit/instructionset.py +++ b/qiskit/circuit/instructionset.py @@ -20,6 +20,7 @@ from typing import Callable from qiskit.circuit.exceptions import CircuitError +from qiskit.utils import deprecate_func from .classicalregister import Clbit, ClassicalRegister from .operation import Operation from .quantumcircuitdata import CircuitInstruction @@ -105,6 +106,7 @@ def inverse(self, annotated: bool = False): ) return self + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0") def c_if(self, classical: Clbit | ClassicalRegister | int, val: int) -> "InstructionSet": """Set a classical equality condition on all the instructions in this set between the :obj:`.ClassicalRegister` or :obj:`.Clbit` ``classical`` and value ``val``. diff --git a/qiskit/circuit/singleton.py b/qiskit/circuit/singleton.py index 874b979ff588..a315f8aaa619 100644 --- a/qiskit/circuit/singleton.py +++ b/qiskit/circuit/singleton.py @@ -251,6 +251,7 @@ class XGate(Gate, metaclass=_SingletonMeta, overrides=_SingletonGateOverrides): import functools +from qiskit.utils import deprecate_func from .instruction import Instruction from .gate import Gate from .controlledgate import ControlledGate, _ctrl_state_to_int @@ -489,6 +490,7 @@ class they are providing overrides for has more lazy attributes or user-exposed instruction._params = _frozenlist(instruction._params) return instruction + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0") def c_if(self, classical, val): return self.to_mutable().c_if(classical, val) diff --git a/qiskit/circuit/store.py b/qiskit/circuit/store.py index 6bbc5439332d..43e81ce61056 100644 --- a/qiskit/circuit/store.py +++ b/qiskit/circuit/store.py @@ -16,6 +16,7 @@ import typing +from qiskit.utils import deprecate_func from .exceptions import CircuitError from .classical import expr, types from .instruction import Instruction @@ -88,6 +89,7 @@ def rvalue(self): """Get the r-value :class:`~.expr.Expr` node that is being written into the l-value.""" return self.params[1] + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0") def c_if(self, classical, val): """:meta hidden:""" raise NotImplementedError( diff --git a/qiskit/qasm2/parse.py b/qiskit/qasm2/parse.py index 6cdb0f70bba0..746866a52f8b 100644 --- a/qiskit/qasm2/parse.py +++ b/qiskit/qasm2/parse.py @@ -11,7 +11,7 @@ # that they have been altered from the originals. """Python-space bytecode interpreter for the output of the main Rust parser logic.""" - +import warnings import dataclasses import math from typing import Iterable, Callable @@ -255,6 +255,11 @@ def from_bytecode(bytecode, custom_instructions: Iterable[CustomInstruction]): CircuitInstruction(gates[gate_id](*parameters), [qubits[q] for q in op_qubits]) ) elif opcode == OpCode.ConditionedGate: + warnings.warn( + "Conditioned gates in qasm2 will be loaded as an IfElseOp starting in Qiskit 2.0", + FutureWarning, + stacklevel=3, + ) gate_id, parameters, op_qubits, creg, value = op.operands gate = gates[gate_id](*parameters).c_if(qc.cregs[creg], value) qc._append(CircuitInstruction(gate, [qubits[q] for q in op_qubits])) @@ -262,12 +267,22 @@ def from_bytecode(bytecode, custom_instructions: Iterable[CustomInstruction]): qubit, clbit = op.operands qc._append(CircuitInstruction(Measure(), (qubits[qubit],), (clbits[clbit],))) elif opcode == OpCode.ConditionedMeasure: + warnings.warn( + "Conditioned measurements in qasm2 will be loaded as an IfElseOp starting in Qiskit 2.0", + FutureWarning, + stacklevel=3, + ) qubit, clbit, creg, value = op.operands measure = Measure().c_if(qc.cregs[creg], value) qc._append(CircuitInstruction(measure, (qubits[qubit],), (clbits[clbit],))) elif opcode == OpCode.Reset: qc._append(CircuitInstruction(Reset(), (qubits[op.operands[0]],))) elif opcode == OpCode.ConditionedReset: + warnings.warn( + "Conditioned resets in qasm2 will be loaded as an IfElseOp starting in Qiskit 2.0", + FutureWarning, + stacklevel=3, + ) qubit, creg, value = op.operands reset = Reset().c_if(qc.cregs[creg], value) qc._append(CircuitInstruction(reset, (qubits[qubit],))) diff --git a/qiskit/qpy/binary_io/circuits.py b/qiskit/qpy/binary_io/circuits.py index 142639a4e164..a9aa46f7dce2 100644 --- a/qiskit/qpy/binary_io/circuits.py +++ b/qiskit/qpy/binary_io/circuits.py @@ -318,6 +318,13 @@ def _read_instruction( use_symengine, standalone_vars, ) + if condition is not None: + warnings.warn( + f"The .condition attribute on {gate_name} will be loaded as an IfElseOp " + "starting in Qiskit 2.0", + FutureWarning, + stacklevel=3, + ) inst_obj.condition = condition if instruction.label_size > 0: inst_obj.label = label @@ -414,6 +421,12 @@ def _read_instruction( gate = gate_class(*params) if condition: if not isinstance(gate, ControlFlowOp): + warnings.warn( + f"The .condition attribute on {gate} will be loaded as an " + "IfElseOp starting in Qiskit 2.0", + FutureWarning, + stacklevel=3, + ) gate = gate.c_if(*condition) else: gate.condition = condition diff --git a/qiskit/transpiler/passes/utils/convert_conditions_to_if_ops.py b/qiskit/transpiler/passes/utils/convert_conditions_to_if_ops.py index a73f9690ae0e..52f56aebbace 100644 --- a/qiskit/transpiler/passes/utils/convert_conditions_to_if_ops.py +++ b/qiskit/transpiler/passes/utils/convert_conditions_to_if_ops.py @@ -22,6 +22,7 @@ ) from qiskit.dagcircuit import DAGCircuit from qiskit.transpiler.basepasses import TransformationPass +from qiskit.utils import deprecate_func class ConvertConditionsToIfOps(TransformationPass): @@ -31,6 +32,10 @@ class ConvertConditionsToIfOps(TransformationPass): This is a simple pass aimed at easing the conversion from the old style of using :meth:`.InstructionSet.c_if` into the new style of using more complex conditional logic.""" + @deprecate_func(since="1.3.0", removal_timeline="in Qiskit 2.0.0") + def __init__(self): + super().__init__() + def _run_inner(self, dag): """Run the pass on one :class:`.DAGCircuit`, mutating it. Returns ``True`` if the circuit was modified and ``False`` if not.""" diff --git a/releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml b/releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml new file mode 100644 index 000000000000..20c5ec38a022 --- /dev/null +++ b/releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml @@ -0,0 +1,38 @@ +--- +deprecations_circuits: + - | + Deprecated the :attr:`.Instruction.condition` and the + :meth:`.Instruction.c_if` method. This attribute and method will be removed + in Qiskit 2.0 and along with any other uses of this in the Qiskit data + model. This functionality has been superseded by the :class:`.IfElseOp` + which can be used to describe a classically condition in a circuit. For + example, a circuit using :attr:`.Instruction.condition` like:: + + from qiskit.circuit import QuantumCircuit + + qc = QuantumCircuit(2, 2) + qc.h(0) + qc.x(0).c_if(0, 1) + qc.z(1.c_if(1, 0) + qc.measure(0, 0) + qc.measure(1, 1) + + can be rewritten using as:: + + qc = QuantumCircuit(2, 2) + qc.h(0) + with expected.if_test((expected.clbits[0], True)): + qc.x(0) + with expected.if_test((expected.clbits[1], False)): + qc.z(1) + qc.measure(0, 0) + qc.measure(1, 1) + + The now deprecated :class:`.ConvertConditionsToIfOps` transpiler pass can + be used to automate this conversion for existing circuits. +deprecations_transpiler: + - | + The transpiler pass :class:`.ConvertConditionsToIfOps` has been deprecated + and will be removed in Qiskit 2.0.0. This class is now deprecated because + the underlying data model for :attr:`.Instruction.condition` which this + pass is converting from has been deprecated and will be removed in 2.0.0. diff --git a/test/python/circuit/classical/test_expr_constructors.py b/test/python/circuit/classical/test_expr_constructors.py index 012697a17dd0..2be64fbef9f3 100644 --- a/test/python/circuit/classical/test_expr_constructors.py +++ b/test/python/circuit/classical/test_expr_constructors.py @@ -26,7 +26,8 @@ def test_lift_legacy_condition(self): clbit = Clbit() inst = Instruction("custom", 1, 0, []) - inst.c_if(cr, 7) + with self.assertWarns(DeprecationWarning): + inst.c_if(cr, 7) self.assertEqual( expr.lift_legacy_condition(inst.condition), expr.Binary( @@ -38,7 +39,8 @@ def test_lift_legacy_condition(self): ) inst = Instruction("custom", 1, 0, []) - inst.c_if(cr, 255) + with self.assertWarns(DeprecationWarning): + inst.c_if(cr, 255) self.assertEqual( expr.lift_legacy_condition(inst.condition), expr.Binary( @@ -50,7 +52,8 @@ def test_lift_legacy_condition(self): ) inst = Instruction("custom", 1, 0, []) - inst.c_if(clbit, False) + with self.assertWarns(DeprecationWarning): + inst.c_if(clbit, False) self.assertEqual( expr.lift_legacy_condition(inst.condition), expr.Unary( @@ -61,7 +64,8 @@ def test_lift_legacy_condition(self): ) inst = Instruction("custom", 1, 0, []) - inst.c_if(clbit, True) + with self.assertWarns(DeprecationWarning): + inst.c_if(clbit, True) self.assertEqual( expr.lift_legacy_condition(inst.condition), expr.Var(clbit, types.Bool()), diff --git a/test/python/circuit/library/test_permutation.py b/test/python/circuit/library/test_permutation.py index a2ec59431fe3..34ae89ce4aa4 100644 --- a/test/python/circuit/library/test_permutation.py +++ b/test/python/circuit/library/test_permutation.py @@ -149,7 +149,8 @@ def test_reverse_ops(self): def test_conditional(self): """Test adding conditional permutations.""" qc = QuantumCircuit(5, 1) - qc.append(PermutationGate([1, 2, 0]), [2, 3, 4]).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.append(PermutationGate([1, 2, 0]), [2, 3, 4]).c_if(0, 1) self.assertIsNotNone(qc.data[0].operation.condition) def test_qasm(self): diff --git a/test/python/circuit/library/test_qft.py b/test/python/circuit/library/test_qft.py index 85837f0ac80c..77510f3cfb9c 100644 --- a/test/python/circuit/library/test_qft.py +++ b/test/python/circuit/library/test_qft.py @@ -272,7 +272,8 @@ def test_reverse_ops(self): def test_conditional(self): """Test adding conditional to a QFTGate.""" qc = QuantumCircuit(5, 1) - qc.append(QFTGate(4), [1, 2, 0, 4]).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.append(QFTGate(4), [1, 2, 0, 4]).c_if(0, 1) self.assertIsNotNone(qc.data[0].operation.condition) def test_qasm(self): diff --git a/test/python/circuit/test_circuit_load_from_qpy.py b/test/python/circuit/test_circuit_load_from_qpy.py index db8663ace558..65d441417d76 100644 --- a/test/python/circuit/test_circuit_load_from_qpy.py +++ b/test/python/circuit/test_circuit_load_from_qpy.py @@ -138,11 +138,13 @@ def test_qpy_full_path(self): def test_circuit_with_conditional(self): """Test that instructions with conditions are correctly serialized.""" qc = QuantumCircuit(1, 1) - qc.x(0).c_if(qc.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(qc.cregs[0], 1) qpy_file = io.BytesIO() dump(qc, qpy_file) qpy_file.seek(0) - new_circ = load(qpy_file)[0] + with self.assertWarns(DeprecationWarning): + new_circ = load(qpy_file)[0] self.assertEqual(qc, new_circ) self.assertDeprecatedBitProperties(qc, new_circ) @@ -408,13 +410,15 @@ def test_multiple_circuits(self): """Test multiple circuits can be serialized together.""" circuits = [] for i in range(10): - circuits.append( - random_circuit(10, 10, measure=True, conditional=True, reset=True, seed=42 + i) - ) + with self.assertWarns(DeprecationWarning): + circuits.append( + random_circuit(10, 10, measure=True, conditional=True, reset=True, seed=42 + i) + ) qpy_file = io.BytesIO() dump(circuits, qpy_file) qpy_file.seek(0) - new_circs = load(qpy_file) + with self.assertWarns(DeprecationWarning): + new_circs = load(qpy_file) self.assertEqual(circuits, new_circs) for old, new in zip(circuits, new_circs): self.assertDeprecatedBitProperties(old, new) @@ -674,12 +678,14 @@ def test_circuit_with_conditional_with_label(self): """Test that instructions with conditions are correctly serialized.""" qc = QuantumCircuit(1, 1) gate = XGate(label="My conditional x gate") - gate.c_if(qc.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + gate.c_if(qc.cregs[0], 1) qc.append(gate, [0]) qpy_file = io.BytesIO() dump(qc, qpy_file) qpy_file.seek(0) - new_circ = load(qpy_file)[0] + with self.assertWarns(DeprecationWarning): + new_circ = load(qpy_file)[0] self.assertEqual(qc, new_circ) self.assertEqual( [x.operation.label for x in qc.data], [x.operation.label for x in new_circ.data] @@ -755,12 +761,14 @@ def test_single_bit_teleportation(self): qc = QuantumCircuit(qr, cr, name="Reset Test") qc.x(0) qc.measure(0, cr[0]) - qc.x(0).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(cr[0], 1) qc.measure(0, cr[1]) qpy_file = io.BytesIO() dump(qc, qpy_file) qpy_file.seek(0) - new_circ = load(qpy_file)[0] + with self.assertWarns(DeprecationWarning): + new_circ = load(qpy_file)[0] self.assertEqual(qc, new_circ) self.assertEqual( [x.operation.label for x in qc.data], [x.operation.label for x in new_circ.data] @@ -1138,11 +1146,13 @@ def test_qpy_with_for_loop(self): qc.h(0) qc.cx(0, 1) qc.measure(0, 0) - qc.break_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.break_loop().c_if(0, True) qpy_file = io.BytesIO() dump(qc, qpy_file) qpy_file.seek(0) - new_circuit = load(qpy_file)[0] + with self.assertWarns(DeprecationWarning): + new_circuit = load(qpy_file)[0] self.assertEqual(qc, new_circuit) self.assertDeprecatedBitProperties(qc, new_circuit) @@ -1154,11 +1164,13 @@ def test_qpy_with_for_loop_iterator(self): qc.h(0) qc.cx(0, 1) qc.measure(0, 0) - qc.break_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.break_loop().c_if(0, True) qpy_file = io.BytesIO() dump(qc, qpy_file) qpy_file.seek(0) - new_circuit = load(qpy_file)[0] + with self.assertWarns(DeprecationWarning): + new_circuit = load(qpy_file)[0] self.assertEqual(qc, new_circuit) self.assertDeprecatedBitProperties(qc, new_circuit) diff --git a/test/python/circuit/test_circuit_operations.py b/test/python/circuit/test_circuit_operations.py index f374f82504a6..7899f5877ff6 100644 --- a/test/python/circuit/test_circuit_operations.py +++ b/test/python/circuit/test_circuit_operations.py @@ -1035,18 +1035,21 @@ def test_repeat(self): qc.h(0) qc.cx(0, 1) qc.barrier() - qc.h(0).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.h(0).c_if(cr, 1) with self.subTest("repeat 0 times"): rep = qc.repeat(0) self.assertEqual(rep, QuantumCircuit(qr, cr)) with self.subTest("repeat 3 times"): - inst = qc.to_instruction() + with self.assertWarns(DeprecationWarning): + inst = qc.to_instruction() ref = QuantumCircuit(qr, cr) for _ in range(3): ref.append(inst, ref.qubits, ref.clbits) - rep = qc.repeat(3) + with self.assertWarns(DeprecationWarning): + rep = qc.repeat(3) self.assertEqual(rep, ref) @data(0, 1, 4) @@ -1292,13 +1295,15 @@ def test_reverse_bits_with_registerless_bits(self): qc = QuantumCircuit([q0, q1], [c0, c1]) qc.h(0) qc.cx(0, 1) - qc.x(0).c_if(1, True) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(1, True) qc.measure(0, 0) expected = QuantumCircuit([c1, c0], [q1, q0]) expected.h(1) expected.cx(1, 0) - expected.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, True) expected.measure(1, 1) self.assertEqual(qc.reverse_bits(), expected) @@ -1389,29 +1394,41 @@ def test_compare_circuits_with_single_bit_conditions(self): qreg = QuantumRegister(1, name="q") creg = ClassicalRegister(1, name="c") qc1 = QuantumCircuit(qreg, creg, [Clbit()]) - qc1.x(0).c_if(qc1.cregs[0], 1) - qc1.x(0).c_if(qc1.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.clbits[-1], True) qc2 = QuantumCircuit(qreg, creg, [Clbit()]) - qc2.x(0).c_if(qc2.cregs[0], 1) - qc2.x(0).c_if(qc2.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.clbits[-1], True) self.assertEqual(qc1, qc2) # Order of operations transposed. qc1 = QuantumCircuit(qreg, creg, [Clbit()]) - qc1.x(0).c_if(qc1.cregs[0], 1) - qc1.x(0).c_if(qc1.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.clbits[-1], True) qc2 = QuantumCircuit(qreg, creg, [Clbit()]) - qc2.x(0).c_if(qc2.clbits[-1], True) - qc2.x(0).c_if(qc2.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.cregs[0], 1) self.assertNotEqual(qc1, qc2) # Single-bit condition values not the same. qc1 = QuantumCircuit(qreg, creg, [Clbit()]) - qc1.x(0).c_if(qc1.cregs[0], 1) - qc1.x(0).c_if(qc1.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.clbits[-1], True) qc2 = QuantumCircuit(qreg, creg, [Clbit()]) - qc2.x(0).c_if(qc2.cregs[0], 1) - qc2.x(0).c_if(qc2.clbits[-1], False) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.clbits[-1], False) self.assertNotEqual(qc1, qc2) def test_compare_a_circuit_with_none(self): diff --git a/test/python/circuit/test_circuit_properties.py b/test/python/circuit/test_circuit_properties.py index d51dd0c75616..fa9e6e5b169d 100644 --- a/test/python/circuit/test_circuit_properties.py +++ b/test/python/circuit/test_circuit_properties.py @@ -226,8 +226,10 @@ def test_circuit_depth_conditionals1(self): qc.cx(q[2], q[3]) qc.measure(q[0], c[0]) qc.measure(q[1], c[1]) - qc.h(q[2]).c_if(c, 2) - qc.h(q[3]).c_if(c, 4) + with self.assertWarns(DeprecationWarning): + qc.h(q[2]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.h(q[3]).c_if(c, 4) self.assertEqual(qc.depth(), 5) def test_circuit_depth_conditionals2(self): @@ -258,8 +260,10 @@ def test_circuit_depth_conditionals2(self): qc.cx(q[2], q[3]) qc.measure(q[0], c[0]) qc.measure(q[0], c[0]) - qc.h(q[2]).c_if(c, 2) - qc.h(q[3]).c_if(c, 4) + with self.assertWarns(DeprecationWarning): + qc.h(q[2]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.h(q[3]).c_if(c, 4) self.assertEqual(qc.depth(), 6) def test_circuit_depth_conditionals3(self): @@ -287,7 +291,8 @@ def test_circuit_depth_conditionals3(self): qc.h(q[2]) qc.h(q[3]) qc.measure(q[0], c[0]) - qc.cx(q[0], q[3]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.cx(q[0], q[3]).c_if(c, 2) qc.measure(q[1], c[1]) qc.measure(q[2], c[2]) @@ -320,8 +325,10 @@ def test_circuit_depth_bit_conditionals1(self): qc.h(q[3]) qc.measure(q[0], c[0]) qc.measure(q[2], c[2]) - qc.h(q[1]).c_if(c[0], True) - qc.h(q[3]).c_if(c[2], False) + with self.assertWarns(DeprecationWarning): + qc.h(q[1]).c_if(c[0], True) + with self.assertWarns(DeprecationWarning): + qc.h(q[3]).c_if(c[2], False) self.assertEqual(qc.depth(), 3) def test_circuit_depth_bit_conditionals2(self): @@ -362,12 +369,18 @@ def test_circuit_depth_bit_conditionals2(self): qc.h(q[3]) qc.measure(q[0], c[0]) qc.measure(q[2], c[2]) - qc.h(q[1]).c_if(c[1], True) - qc.h(q[3]).c_if(c[3], True) - qc.cx(0, 1).c_if(c[0], False) - qc.cx(2, 3).c_if(c[2], False) - qc.ch(0, 2).c_if(c[1], True) - qc.ch(1, 3).c_if(c[3], True) + with self.assertWarns(DeprecationWarning): + qc.h(q[1]).c_if(c[1], True) + with self.assertWarns(DeprecationWarning): + qc.h(q[3]).c_if(c[3], True) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(c[0], False) + with self.assertWarns(DeprecationWarning): + qc.cx(2, 3).c_if(c[2], False) + with self.assertWarns(DeprecationWarning): + qc.ch(0, 2).c_if(c[1], True) + with self.assertWarns(DeprecationWarning): + qc.ch(1, 3).c_if(c[3], True) self.assertEqual(qc.depth(), 4) def test_circuit_depth_bit_conditionals3(self): @@ -395,9 +408,12 @@ def test_circuit_depth_bit_conditionals3(self): qc.h(q[2]) qc.h(q[3]) qc.measure(q[0], c[0]) - qc.h(1).c_if(c[0], True) - qc.h(q[2]).c_if(c, 2) - qc.h(3).c_if(c[3], True) + with self.assertWarns(DeprecationWarning): + qc.h(1).c_if(c[0], True) + with self.assertWarns(DeprecationWarning): + qc.h(q[2]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.h(3).c_if(c[3], True) qc.measure(q[1], c[1]) qc.measure(q[2], c[2]) qc.measure(q[3], c[3]) @@ -608,7 +624,8 @@ def test_circuit_depth_multiqubit_or_conditional(self): circ.rz(0.1, 1) circ.cz(1, 3) circ.measure(1, 0) - circ.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + circ.x(0).c_if(0, 1) self.assertEqual( circ.depth(lambda x: x.operation.num_qubits >= 2 or x.operation.condition is not None), 4, @@ -765,7 +782,8 @@ def test_circuit_nonlocal_gates(self): qc.cry(0.1, q[2], q[4]) qc.z(q[3:]) qc.cswap(q[1], q[2], q[3]) - qc.iswap(q[0], q[4]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.iswap(q[0], q[4]).c_if(c, 2) result = qc.num_nonlocal_gates() expected = 3 self.assertEqual(expected, result) @@ -921,7 +939,8 @@ def test_circuit_connected_components_with_cond(self): qc.h(q[2]) qc.h(q[3]) qc.measure(q[0], c[0]) - qc.cx(q[0], q[3]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.cx(q[0], q[3]).c_if(c, 2) qc.measure(q[1], c[1]) qc.measure(q[2], c[2]) qc.measure(q[3], c[3]) @@ -949,8 +968,10 @@ def test_circuit_connected_components_with_cond2(self): qc.h(q[1]) qc.h(q[2]) qc.h(q[3]) - qc.h(0).c_if(c, 0) - qc.cx(1, 2).c_if(c, 4) + with self.assertWarns(DeprecationWarning): + qc.h(0).c_if(c, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(1, 2).c_if(c, 4) self.assertEqual(qc.num_connected_components(), 2) def test_circuit_connected_components_with_cond3(self): @@ -977,10 +998,13 @@ def test_circuit_connected_components_with_cond3(self): qc.h(q[2]) qc.h(q[3]) qc.measure(q[0], c[0]) - qc.h(q[0]).c_if(c, 0) - qc.cx(q[1], q[2]).c_if(c, 1) + with self.assertWarns(DeprecationWarning): + qc.h(q[0]).c_if(c, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(q[1], q[2]).c_if(c, 1) qc.measure(q[2], c[2]) - qc.x(q[3]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.x(q[3]).c_if(c, 2) self.assertEqual(qc.num_connected_components(), 1) def test_circuit_connected_components_with_bit_cond(self): @@ -1007,7 +1031,8 @@ def test_circuit_connected_components_with_bit_cond(self): qc.h(q[2]) qc.h(q[3]) qc.measure(q[0], c[0]) - qc.cx(q[0], q[3]).c_if(c[0], True) + with self.assertWarns(DeprecationWarning): + qc.cx(q[0], q[3]).c_if(c[0], True) qc.measure(q[1], c[1]) qc.measure(q[2], c[2]) qc.measure(q[3], c[3]) @@ -1035,9 +1060,12 @@ def test_circuit_connected_components_with_bit_cond2(self): qc.h(q[1]) qc.h(q[2]) qc.h(q[3]) - qc.h(0).c_if(c[1], True) - qc.cx(1, 0).c_if(c[4], False) - qc.cz(2, 3).c_if(c[0], True) + with self.assertWarns(DeprecationWarning): + qc.h(0).c_if(c[1], True) + with self.assertWarns(DeprecationWarning): + qc.cx(1, 0).c_if(c[4], False) + with self.assertWarns(DeprecationWarning): + qc.cz(2, 3).c_if(c[0], True) self.assertEqual(qc.num_connected_components(), 5) def test_circuit_connected_components_with_bit_cond3(self): @@ -1063,9 +1091,12 @@ def test_circuit_connected_components_with_bit_cond3(self): qc.h(q[1]) qc.h(q[2]) qc.h(q[3]) - qc.h(q[0]).c_if(c[0], True) - qc.cx(q[1], q[2]).c_if(c, 1) - qc.x(q[3]).c_if(c[2], True) + with self.assertWarns(DeprecationWarning): + qc.h(q[0]).c_if(c[0], True) + with self.assertWarns(DeprecationWarning): + qc.cx(q[1], q[2]).c_if(c, 1) + with self.assertWarns(DeprecationWarning): + qc.x(q[3]).c_if(c[2], True) self.assertEqual(qc.num_connected_components(), 1) def test_circuit_unitary_factors1(self): @@ -1109,7 +1140,8 @@ def test_circuit_unitary_factors3(self): qc.h(q[3]) qc.cx(q[1], q[2]) qc.cx(q[1], q[2]) - qc.cx(q[0], q[3]).c_if(c, 2) + with self.assertWarns(DeprecationWarning): + qc.cx(q[0], q[3]).c_if(c, 2) qc.cx(q[0], q[3]) qc.cx(q[0], q[3]) qc.cx(q[0], q[3]) diff --git a/test/python/circuit/test_circuit_qasm.py b/test/python/circuit/test_circuit_qasm.py index 984851e00388..bbbb494877f6 100644 --- a/test/python/circuit/test_circuit_qasm.py +++ b/test/python/circuit/test_circuit_qasm.py @@ -44,9 +44,12 @@ def test_circuit_qasm(self): qc.barrier(qr2) qc.cx(qr2[1], qr1[0]) qc.h(qr2[1]) - qc.x(qr2[1]).c_if(cr, 0) - qc.y(qr1[0]).c_if(cr, 1) - qc.z(qr1[0]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + qc.x(qr2[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.y(qr1[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.z(qr1[0]).c_if(cr, 2) qc.barrier(qr1, qr2) qc.measure(qr1[0], cr[0]) qc.measure(qr2[0], cr[1]) @@ -639,7 +642,8 @@ def test_circuit_raises_on_single_bit_condition(self): """OpenQASM 2 can't represent single-bit conditions, so test that a suitable error is printed if this is attempted.""" qc = QuantumCircuit(1, 1) - qc.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, True) with self.assertRaisesRegex(QasmError, "OpenQASM 2 can only condition on registers"): dumps(qc) diff --git a/test/python/circuit/test_commutation_checker.py b/test/python/circuit/test_commutation_checker.py index a0aeae5ca2c3..b7cd73e03300 100644 --- a/test/python/circuit/test_commutation_checker.py +++ b/test/python/circuit/test_commutation_checker.py @@ -252,26 +252,33 @@ def test_conditional_gates(self): # Currently, in all cases commutativity checker should returns False. # This is definitely suboptimal. - self.assertFalse( - scc.commute(CXGate().c_if(cr[0], 0), [qr[0], qr[1]], [], XGate(), [qr[2]], []) - ) - self.assertFalse( - scc.commute(CXGate().c_if(cr[0], 0), [qr[0], qr[1]], [], XGate(), [qr[1]], []) - ) - self.assertFalse( - scc.commute( - CXGate().c_if(cr[0], 0), - [qr[0], qr[1]], - [], - CXGate().c_if(cr[0], 0), - [qr[0], qr[1]], - [], + with self.assertWarns(DeprecationWarning): + self.assertFalse( + scc.commute(CXGate().c_if(cr[0], 0), [qr[0], qr[1]], [], XGate(), [qr[2]], []) ) - ) - self.assertFalse( - scc.commute(XGate().c_if(cr[0], 0), [qr[0]], [], XGate().c_if(cr[0], 1), [qr[0]], []) - ) - self.assertFalse(scc.commute(XGate().c_if(cr[0], 0), [qr[0]], [], XGate(), [qr[0]], [])) + with self.assertWarns(DeprecationWarning): + self.assertFalse( + scc.commute(CXGate().c_if(cr[0], 0), [qr[0], qr[1]], [], XGate(), [qr[1]], []) + ) + with self.assertWarns(DeprecationWarning): + self.assertFalse( + scc.commute( + CXGate().c_if(cr[0], 0), + [qr[0], qr[1]], + [], + CXGate().c_if(cr[0], 0), + [qr[0], qr[1]], + [], + ) + ) + with self.assertWarns(DeprecationWarning): + self.assertFalse( + scc.commute( + XGate().c_if(cr[0], 0), [qr[0]], [], XGate().c_if(cr[0], 1), [qr[0]], [] + ) + ) + with self.assertWarns(DeprecationWarning): + self.assertFalse(scc.commute(XGate().c_if(cr[0], 0), [qr[0]], [], XGate(), [qr[0]], [])) def test_complex_gates(self): """Check commutativity involving more complex gates.""" diff --git a/test/python/circuit/test_compose.py b/test/python/circuit/test_compose.py index 582bee082d99..cc4be3c3ffa2 100644 --- a/test/python/circuit/test_compose.py +++ b/test/python/circuit/test_compose.py @@ -367,7 +367,8 @@ def __init__(self): super().__init__("mygate", 1, []) conditional = QuantumCircuit(1, 1) - conditional.append(Custom(), [0], []).c_if(conditional.clbits[0], True) + with self.assertWarns(DeprecationWarning): + conditional.append(Custom(), [0], []).c_if(conditional.clbits[0], True) test = base.compose(conditional, qubits=[0], clbits=[0], copy=False) self.assertIs(test.data[-1].operation, conditional.data[-1].operation) self.assertEqual(test.data[-1].operation.condition, (test.clbits[0], True)) @@ -463,16 +464,20 @@ def test_compose_conditional(self): creg = ClassicalRegister(2, "rcr") circuit_right = QuantumCircuit(qreg, creg) - circuit_right.x(qreg[1]).c_if(creg, 3) - circuit_right.h(qreg[0]).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + circuit_right.x(qreg[1]).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + circuit_right.h(qreg[0]).c_if(creg, 3) circuit_right.measure(qreg, creg) # permuted subset of qubits and clbits circuit_composed = self.circuit_left.compose(circuit_right, qubits=[1, 4], clbits=[0, 1]) circuit_expected = self.circuit_left.copy() - circuit_expected.x(self.left_qubit4).c_if(*self.condition) - circuit_expected.h(self.left_qubit1).c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + circuit_expected.x(self.left_qubit4).c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + circuit_expected.h(self.left_qubit1).c_if(*self.condition) circuit_expected.measure(self.left_qubit1, self.left_clbit0) circuit_expected.measure(self.left_qubit4, self.left_clbit1) @@ -489,8 +494,10 @@ def test_compose_conditional_no_match(self): right.cx(0, 1) right.h(0) right.measure([0, 1], [0, 1]) - right.z(2).c_if(right.cregs[0], 1) - right.x(2).c_if(right.cregs[1], 1) + with self.assertWarns(DeprecationWarning): + right.z(2).c_if(right.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + right.x(2).c_if(right.cregs[1], 1) test = QuantumCircuit(3, 3).compose(right, range(3), range(2)) z = next(ins.operation for ins in test.data[::-1] if ins.operation.name == "z") x = next(ins.operation for ins in test.data[::-1] if ins.operation.name == "x") @@ -724,7 +731,8 @@ 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) + with self.assertWarns(DeprecationWarning): + 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) diff --git a/test/python/circuit/test_control_flow.py b/test/python/circuit/test_control_flow.py index 35c57839d753..cdb87ac0d342 100644 --- a/test/python/circuit/test_control_flow.py +++ b/test/python/circuit/test_control_flow.py @@ -808,15 +808,20 @@ def test_no_c_if_for_while_loop_if_else(self): body = QuantumCircuit(1) with self.assertRaisesRegex(NotImplementedError, r"cannot be classically controlled"): - qc.while_loop((qc.clbits[0], False), body, [qc.qubits[0]], []).c_if(qc.clbits[0], True) + with self.assertWarns(DeprecationWarning): + qc.while_loop((qc.clbits[0], False), body, [qc.qubits[0]], []).c_if( + qc.clbits[0], True + ) with self.assertRaisesRegex(NotImplementedError, r"cannot be classically controlled"): - qc.if_test((qc.clbits[0], False), body, [qc.qubits[0]], []).c_if(qc.clbits[0], True) + with self.assertWarns(DeprecationWarning): + qc.if_test((qc.clbits[0], False), body, [qc.qubits[0]], []).c_if(qc.clbits[0], True) with self.assertRaisesRegex(NotImplementedError, r"cannot be classically controlled"): - qc.if_else((qc.clbits[0], False), body, body, [qc.qubits[0]], []).c_if( - qc.clbits[0], True - ) + with self.assertWarns(DeprecationWarning): + qc.if_else((qc.clbits[0], False), body, body, [qc.qubits[0]], []).c_if( + qc.clbits[0], True + ) def test_nested_parameters_are_recognised(self): """Verify that parameters added inside a control-flow operator get added to the outer diff --git a/test/python/circuit/test_control_flow_builders.py b/test/python/circuit/test_control_flow_builders.py index 7a3a0873ae77..419cfb406d19 100644 --- a/test/python/circuit/test_control_flow_builders.py +++ b/test/python/circuit/test_control_flow_builders.py @@ -193,12 +193,16 @@ def test_register_condition_in_nested_block(self): with self.subTest("if/c_if"): test = QuantumCircuit(qr, clbits, cr1, cr2, cr3, cr4) with test.if_test((cr1, 0)): - test.x(0).c_if(cr2, 0) - test.z(0).c_if(cr3, 0) + with self.assertWarns(DeprecationWarning): + test.x(0).c_if(cr2, 0) + with self.assertWarns(DeprecationWarning): + test.z(0).c_if(cr3, 0) true_body = QuantumCircuit([qr[0]], clbits, cr1, cr2, cr3) - true_body.x(0).c_if(cr2, 0) - true_body.z(0).c_if(cr3, 0) + with self.assertWarns(DeprecationWarning): + true_body.x(0).c_if(cr2, 0) + with self.assertWarns(DeprecationWarning): + true_body.z(0).c_if(cr3, 0) expected = QuantumCircuit(qr, clbits, cr1, cr2, cr3, cr4) expected.if_test((cr1, 0), true_body, [qr[0]], clbits + list(cr1)) @@ -209,14 +213,18 @@ def test_register_condition_in_nested_block(self): test = QuantumCircuit(qr, clbits, cr1, cr2, cr3, cr4) with test.while_loop((cr1, 0)): with test.if_test((cr2, 0)) as else_: - test.x(0).c_if(cr3, 0) + with self.assertWarns(DeprecationWarning): + test.x(0).c_if(cr3, 0) with else_: - test.z(0).c_if(cr4, 0) + with self.assertWarns(DeprecationWarning): + test.z(0).c_if(cr4, 0) true_body = QuantumCircuit([qr[0]], cr2, cr3, cr4) - true_body.x(0).c_if(cr3, 0) + with self.assertWarns(DeprecationWarning): + true_body.x(0).c_if(cr3, 0) false_body = QuantumCircuit([qr[0]], cr2, cr3, cr4) - false_body.z(0).c_if(cr4, 0) + with self.assertWarns(DeprecationWarning): + false_body.z(0).c_if(cr4, 0) while_body = QuantumCircuit([qr[0]], clbits, cr1, cr2, cr3, cr4) while_body.if_else((cr2, 0), true_body, false_body, [qr[0]], clbits) @@ -647,17 +655,23 @@ def test_if_else_tracks_registers(self): test = QuantumCircuit(qr, *cr) with test.if_test((cr[0], 0)) as else_: - test.h(0).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(cr[1], 0) # Test repetition. - test.h(0).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(cr[1], 0) with else_: - test.h(0).c_if(cr[2], 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(cr[2], 0) true_body = QuantumCircuit([qr[0]], cr[0], cr[1], cr[2]) - true_body.h(qr[0]).c_if(cr[1], 0) - true_body.h(qr[0]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + true_body.h(qr[0]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + true_body.h(qr[0]).c_if(cr[1], 0) false_body = QuantumCircuit([qr[0]], cr[0], cr[1], cr[2]) - false_body.h(qr[0]).c_if(cr[2], 0) + with self.assertWarns(DeprecationWarning): + false_body.h(qr[0]).c_if(cr[2], 0) expected = QuantumCircuit(qr, *cr) expected.if_else( @@ -1036,11 +1050,13 @@ def test_break_continue_accept_c_if(self, loop_operation): test = QuantumCircuit(qubits, clbits) with test.for_loop(range(2)): test.h(0) - loop_operation(test).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + loop_operation(test).c_if(1, 0) body = QuantumCircuit([qubits[0]], [clbits[1]]) body.h(qubits[0]) - loop_operation(body).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + loop_operation(body).c_if(clbits[1], 0) expected = QuantumCircuit(qubits, clbits) expected.for_loop(range(2), None, body, [qubits[0]], [clbits[1]]) @@ -1052,11 +1068,13 @@ def test_break_continue_accept_c_if(self, loop_operation): test = QuantumCircuit(qubits, clbits) with test.while_loop(cond): test.h(0) - loop_operation(test).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + loop_operation(test).c_if(1, 0) body = QuantumCircuit([qubits[0]], clbits) body.h(qubits[0]) - loop_operation(body).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + loop_operation(body).c_if(clbits[1], 0) expected = QuantumCircuit(qubits, clbits) expected.while_loop(cond, body, [qubits[0]], clbits) @@ -1230,7 +1248,8 @@ def test_break_continue_nested_in_if(self, loop_operation): # full width of the loop do so. with test.if_test(cond_inner): pass - test.h(0).c_if(2, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(2, 0) true_body1 = QuantumCircuit([qubits[0], clbits[0], clbits[2]]) loop_operation(true_body1) @@ -1240,7 +1259,8 @@ def test_break_continue_nested_in_if(self, loop_operation): loop_body = QuantumCircuit([qubits[0], clbits[0], clbits[2]]) loop_body.if_test(cond_inner, true_body1, [qubits[0]], [clbits[0], clbits[2]]) loop_body.if_test(cond_inner, true_body2, [], [clbits[0]]) - loop_body.h(qubits[0]).c_if(clbits[2], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[0]).c_if(clbits[2], 0) expected = QuantumCircuit(qubits, clbits) expected.for_loop(range(2), None, loop_body, [qubits[0]], [clbits[0], clbits[2]]) @@ -1258,7 +1278,8 @@ def test_break_continue_nested_in_if(self, loop_operation): pass with else_: pass - test.h(0).c_if(2, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(2, 0) true_body1 = QuantumCircuit([qubits[0], qubits[1], clbits[0], clbits[2]]) true_body1.h(qubits[1]) @@ -1273,7 +1294,8 @@ def test_break_continue_nested_in_if(self, loop_operation): cond_inner, true_body1, false_body1, [qubits[0], qubits[1]], [clbits[0], clbits[2]] ) loop_body.if_else(cond_inner, true_body2, false_body2, [], [clbits[0]]) - loop_body.h(qubits[0]).c_if(clbits[2], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[0]).c_if(clbits[2], 0) expected = QuantumCircuit(qubits, clbits) expected.for_loop( @@ -1289,7 +1311,8 @@ def test_break_continue_nested_in_if(self, loop_operation): loop_operation(test) with test.if_test(cond_inner): pass - test.h(0).c_if(2, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(2, 0) true_body1 = QuantumCircuit([qubits[0], clbits[0], clbits[1], clbits[2]]) loop_operation(true_body1) @@ -1301,7 +1324,8 @@ def test_break_continue_nested_in_if(self, loop_operation): cond_inner, true_body1, [qubits[0]], [clbits[0], clbits[1], clbits[2]] ) loop_body.if_test(cond_inner, true_body2, [], [clbits[0]]) - loop_body.h(qubits[0]).c_if(clbits[2], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[0]).c_if(clbits[2], 0) expected = QuantumCircuit(qubits, clbits) expected.while_loop( @@ -1321,7 +1345,8 @@ def test_break_continue_nested_in_if(self, loop_operation): pass with else_: pass - test.h(0).c_if(2, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(2, 0) true_body1 = QuantumCircuit([qubits[0], qubits[1], clbits[0], clbits[1], clbits[2]]) true_body1.h(qubits[1]) @@ -1340,7 +1365,8 @@ def test_break_continue_nested_in_if(self, loop_operation): [clbits[0], clbits[1], clbits[2]], ) loop_body.if_else(cond_inner, true_body2, false_body2, [], [clbits[0]]) - loop_body.h(qubits[0]).c_if(clbits[2], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[0]).c_if(clbits[2], 0) expected = QuantumCircuit(qubits, clbits) expected.while_loop( @@ -1368,7 +1394,8 @@ def test_break_continue_nested_in_switch(self, loop_operation): with test.switch(clbits[0]) as case: with case(case.DEFAULT): pass - test.h(0).c_if(clbits[2], 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(clbits[2], 0) body0 = QuantumCircuit([qubits[0], clbits[0], clbits[2]]) loop_operation(body0) @@ -1379,7 +1406,8 @@ def test_break_continue_nested_in_switch(self, loop_operation): loop_body = QuantumCircuit([qubits[0], clbits[0], clbits[2]]) loop_body.switch(clbits[0], [(0, body0), (1, body1)], [qubits[0]], [clbits[0], clbits[2]]) loop_body.switch(clbits[0], [(CASE_DEFAULT, body2)], [], [clbits[0]]) - loop_body.h(qubits[0]).c_if(clbits[2], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[0]).c_if(clbits[2], 0) expected = QuantumCircuit(qubits, clbits) expected.for_loop(range(2), None, loop_body, [qubits[0]], [clbits[0], clbits[2]]) @@ -1465,18 +1493,23 @@ def test_break_continue_deeply_nested(self, loop_operation): loop_operation(test) # inner true 2 with test.if_test(cond_inner): - test.h(0).c_if(3, 0) - test.h(1).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(4, 0) # outer true 2 with test.if_test(cond_outer): - test.h(2).c_if(5, 0) - test.h(3).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(6, 0) inner_true_body1 = QuantumCircuit(qubits[:4], clbits[:2], clbits[3:7]) loop_operation(inner_true_body1) inner_true_body2 = QuantumCircuit([qubits[0], clbits[0], clbits[3]]) - inner_true_body2.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + inner_true_body2.h(qubits[0]).c_if(clbits[3], 0) outer_true_body1 = QuantumCircuit(qubits[:4], clbits[:2], clbits[3:7]) outer_true_body1.if_test( @@ -1485,15 +1518,18 @@ def test_break_continue_deeply_nested(self, loop_operation): outer_true_body1.if_test( cond_inner, inner_true_body2, [qubits[0]], [clbits[0], clbits[3]] ) - outer_true_body1.h(qubits[1]).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + outer_true_body1.h(qubits[1]).c_if(clbits[4], 0) outer_true_body2 = QuantumCircuit([qubits[2], clbits[1], clbits[5]]) - outer_true_body2.h(qubits[2]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + outer_true_body2.h(qubits[2]).c_if(clbits[5], 0) loop_body = QuantumCircuit(qubits[:4], clbits[:2] + clbits[3:7]) loop_body.if_test(cond_outer, outer_true_body1, qubits[:4], clbits[:2] + clbits[3:7]) loop_body.if_test(cond_outer, outer_true_body2, [qubits[2]], [clbits[1], clbits[5]]) - loop_body.h(qubits[3]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[3]).c_if(clbits[6], 0) expected = QuantumCircuit(qubits, clbits) expected.for_loop(range(2), None, loop_body, qubits[:4], clbits[:2] + clbits[3:7]) @@ -1507,31 +1543,43 @@ def test_break_continue_deeply_nested(self, loop_operation): with test.if_test(cond_outer): # inner 1 with test.if_test(cond_inner) as inner1_else: - test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) with inner1_else: - loop_operation(test).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + loop_operation(test).c_if(4, 0) # inner 2 with test.if_test(cond_inner) as inner2_else: - test.h(1).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(5, 0) with inner2_else: - test.h(2).c_if(6, 0) - test.h(3).c_if(7, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(7, 0) # outer 2 with test.if_test(cond_outer) as outer2_else: - test.h(4).c_if(8, 0) + with self.assertWarns(DeprecationWarning): + test.h(4).c_if(8, 0) with outer2_else: - test.h(5).c_if(9, 0) - test.h(6).c_if(10, 0) + with self.assertWarns(DeprecationWarning): + test.h(5).c_if(9, 0) + with self.assertWarns(DeprecationWarning): + test.h(6).c_if(10, 0) inner1_true = QuantumCircuit(qubits[:7], clbits[:2], clbits[3:11]) - inner1_true.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + inner1_true.h(qubits[0]).c_if(clbits[3], 0) inner1_false = QuantumCircuit(qubits[:7], clbits[:2], clbits[3:11]) - loop_operation(inner1_false).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + loop_operation(inner1_false).c_if(clbits[4], 0) inner2_true = QuantumCircuit([qubits[1], qubits[2], clbits[0], clbits[5], clbits[6]]) - inner2_true.h(qubits[1]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + inner2_true.h(qubits[1]).c_if(clbits[5], 0) inner2_false = QuantumCircuit([qubits[1], qubits[2], clbits[0], clbits[5], clbits[6]]) - inner2_false.h(qubits[2]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + inner2_false.h(qubits[2]).c_if(clbits[6], 0) outer1_true = QuantumCircuit(qubits[:7], clbits[:2], clbits[3:11]) outer1_true.if_else( @@ -1544,12 +1592,15 @@ def test_break_continue_deeply_nested(self, loop_operation): qubits[1:3], [clbits[0], clbits[5], clbits[6]], ) - outer1_true.h(qubits[3]).c_if(clbits[7], 0) + with self.assertWarns(DeprecationWarning): + outer1_true.h(qubits[3]).c_if(clbits[7], 0) outer2_true = QuantumCircuit([qubits[4], qubits[5], clbits[1], clbits[8], clbits[9]]) - outer2_true.h(qubits[4]).c_if(clbits[8], 0) + with self.assertWarns(DeprecationWarning): + outer2_true.h(qubits[4]).c_if(clbits[8], 0) outer2_false = QuantumCircuit([qubits[4], qubits[5], clbits[1], clbits[8], clbits[9]]) - outer2_false.h(qubits[5]).c_if(clbits[9], 0) + with self.assertWarns(DeprecationWarning): + outer2_false.h(qubits[5]).c_if(clbits[9], 0) loop_body = QuantumCircuit(qubits[:7], clbits[:2], clbits[3:11]) loop_body.if_test(cond_outer, outer1_true, qubits[:7], clbits[:2] + clbits[3:11]) @@ -1560,7 +1611,8 @@ def test_break_continue_deeply_nested(self, loop_operation): qubits[4:6], [clbits[1], clbits[8], clbits[9]], ) - loop_body.h(qubits[6]).c_if(clbits[10], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[6]).c_if(clbits[10], 0) expected = QuantumCircuit(qubits, clbits) expected.for_loop(range(2), None, loop_body, qubits[:7], clbits[:2] + clbits[3:11]) @@ -1574,72 +1626,90 @@ def test_break_continue_deeply_nested(self, loop_operation): test = QuantumCircuit(qubits, clbits) with test.for_loop(range(2)): - test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) # outer 1 with test.if_test(cond_outer) as outer1_else: - test.h(1).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(4, 0) with outer1_else: - test.h(2).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(5, 0) # outer 2 (nesting the inner condition in the 'if') with test.if_test(cond_outer) as outer2_else: - test.h(3).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(6, 0) # inner 21 with test.if_test(cond_inner) as inner21_else: loop_operation(test) with inner21_else: - test.h(4).c_if(7, 0) + with self.assertWarns(DeprecationWarning): + test.h(4).c_if(7, 0) # inner 22 with test.if_test(cond_inner) as inner22_else: - test.h(5).c_if(8, 0) + with self.assertWarns(DeprecationWarning): + test.h(5).c_if(8, 0) with inner22_else: loop_operation(test) # inner 23 with test.switch(cond_inner[0]) as inner23_case: with inner23_case(True): - test.h(5).c_if(8, 0) + with self.assertWarns(DeprecationWarning): + test.h(5).c_if(8, 0) with inner23_case(False): loop_operation(test) - test.h(6).c_if(9, 0) + with self.assertWarns(DeprecationWarning): + test.h(6).c_if(9, 0) with outer2_else: - test.h(7).c_if(10, 0) + with self.assertWarns(DeprecationWarning): + test.h(7).c_if(10, 0) # inner 24 with test.if_test(cond_inner) as inner24_else: - test.h(8).c_if(11, 0) + with self.assertWarns(DeprecationWarning): + test.h(8).c_if(11, 0) with inner24_else: - test.h(9).c_if(12, 0) + with self.assertWarns(DeprecationWarning): + test.h(9).c_if(12, 0) # outer 3 (nesting the inner condition in an 'else' branch) with test.if_test(cond_outer) as outer3_else: - test.h(10).c_if(13, 0) + with self.assertWarns(DeprecationWarning): + test.h(10).c_if(13, 0) with outer3_else: - test.h(11).c_if(14, 0) + with self.assertWarns(DeprecationWarning): + test.h(11).c_if(14, 0) # inner 31 with test.if_test(cond_inner) as inner31_else: loop_operation(test) with inner31_else: - test.h(12).c_if(15, 0) + with self.assertWarns(DeprecationWarning): + test.h(12).c_if(15, 0) # inner 32 with test.if_test(cond_inner) as inner32_else: - test.h(13).c_if(16, 0) + with self.assertWarns(DeprecationWarning): + test.h(13).c_if(16, 0) with inner32_else: loop_operation(test) # inner 33 with test.if_test(cond_inner) as inner33_else: - test.h(14).c_if(17, 0) + with self.assertWarns(DeprecationWarning): + test.h(14).c_if(17, 0) with inner33_else: - test.h(15).c_if(18, 0) + with self.assertWarns(DeprecationWarning): + test.h(15).c_if(18, 0) - test.h(16).c_if(19, 0) + with self.assertWarns(DeprecationWarning): + test.h(16).c_if(19, 0) # End of test "for" loop. # No `clbits[2]` here because that's only used in `cond_loop`, for while loops. @@ -1648,32 +1718,40 @@ def test_break_continue_deeply_nested(self, loop_operation): loop_bits = loop_qubits + loop_clbits outer1_true = QuantumCircuit([qubits[1], qubits[2], clbits[1], clbits[4], clbits[5]]) - outer1_true.h(qubits[1]).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + outer1_true.h(qubits[1]).c_if(clbits[4], 0) outer1_false = QuantumCircuit([qubits[1], qubits[2], clbits[1], clbits[4], clbits[5]]) - outer1_false.h(qubits[2]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + outer1_false.h(qubits[2]).c_if(clbits[5], 0) inner21_true = QuantumCircuit(loop_bits) loop_operation(inner21_true) inner21_false = QuantumCircuit(loop_bits) - inner21_false.h(qubits[4]).c_if(clbits[7], 0) + with self.assertWarns(DeprecationWarning): + inner21_false.h(qubits[4]).c_if(clbits[7], 0) inner22_true = QuantumCircuit(loop_bits) - inner22_true.h(qubits[5]).c_if(clbits[8], 0) + with self.assertWarns(DeprecationWarning): + inner22_true.h(qubits[5]).c_if(clbits[8], 0) inner22_false = QuantumCircuit(loop_bits) loop_operation(inner22_false) inner23_true = QuantumCircuit(loop_bits) - inner23_true.h(qubits[5]).c_if(clbits[8], 0) + with self.assertWarns(DeprecationWarning): + inner23_true.h(qubits[5]).c_if(clbits[8], 0) inner23_false = QuantumCircuit(loop_bits) loop_operation(inner23_false) inner24_true = QuantumCircuit(qubits[8:10], [clbits[0], clbits[11], clbits[12]]) - inner24_true.h(qubits[8]).c_if(clbits[11], 0) + with self.assertWarns(DeprecationWarning): + inner24_true.h(qubits[8]).c_if(clbits[11], 0) inner24_false = QuantumCircuit(qubits[8:10], [clbits[0], clbits[11], clbits[12]]) - inner24_false.h(qubits[9]).c_if(clbits[12], 0) + with self.assertWarns(DeprecationWarning): + inner24_false.h(qubits[9]).c_if(clbits[12], 0) outer2_true = QuantumCircuit(loop_bits) - outer2_true.h(qubits[3]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + outer2_true.h(qubits[3]).c_if(clbits[6], 0) outer2_true.if_else(cond_inner, inner21_true, inner21_false, loop_qubits, loop_clbits) outer2_true.if_else(cond_inner, inner22_true, inner22_false, loop_qubits, loop_clbits) outer2_true.switch( @@ -1682,9 +1760,11 @@ def test_break_continue_deeply_nested(self, loop_operation): loop_qubits, loop_clbits, ) - outer2_true.h(qubits[6]).c_if(clbits[9], 0) + with self.assertWarns(DeprecationWarning): + outer2_true.h(qubits[6]).c_if(clbits[9], 0) outer2_false = QuantumCircuit(loop_bits) - outer2_false.h(qubits[7]).c_if(clbits[10], 0) + with self.assertWarns(DeprecationWarning): + outer2_false.h(qubits[7]).c_if(clbits[10], 0) outer2_false.if_else( cond_inner, inner24_true, @@ -1696,22 +1776,28 @@ def test_break_continue_deeply_nested(self, loop_operation): inner31_true = QuantumCircuit(loop_bits) loop_operation(inner31_true) inner31_false = QuantumCircuit(loop_bits) - inner31_false.h(qubits[12]).c_if(clbits[15], 0) + with self.assertWarns(DeprecationWarning): + inner31_false.h(qubits[12]).c_if(clbits[15], 0) inner32_true = QuantumCircuit(loop_bits) - inner32_true.h(qubits[13]).c_if(clbits[16], 0) + with self.assertWarns(DeprecationWarning): + inner32_true.h(qubits[13]).c_if(clbits[16], 0) inner32_false = QuantumCircuit(loop_bits) loop_operation(inner32_false) inner33_true = QuantumCircuit(qubits[14:16], [clbits[0], clbits[17], clbits[18]]) - inner33_true.h(qubits[14]).c_if(clbits[17], 0) + with self.assertWarns(DeprecationWarning): + inner33_true.h(qubits[14]).c_if(clbits[17], 0) inner33_false = QuantumCircuit(qubits[14:16], [clbits[0], clbits[17], clbits[18]]) - inner33_false.h(qubits[15]).c_if(clbits[18], 0) + with self.assertWarns(DeprecationWarning): + inner33_false.h(qubits[15]).c_if(clbits[18], 0) outer3_true = QuantumCircuit(loop_bits) - outer3_true.h(qubits[10]).c_if(clbits[13], 0) + with self.assertWarns(DeprecationWarning): + outer3_true.h(qubits[10]).c_if(clbits[13], 0) outer3_false = QuantumCircuit(loop_bits) - outer3_false.h(qubits[11]).c_if(clbits[14], 0) + with self.assertWarns(DeprecationWarning): + outer3_false.h(qubits[11]).c_if(clbits[14], 0) outer3_false.if_else(cond_inner, inner31_true, inner31_false, loop_qubits, loop_clbits) outer3_false.if_else(cond_inner, inner32_true, inner32_false, loop_qubits, loop_clbits) outer3_false.if_else( @@ -1723,7 +1809,8 @@ def test_break_continue_deeply_nested(self, loop_operation): ) loop_body = QuantumCircuit(loop_bits) - loop_body.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[0]).c_if(clbits[3], 0) loop_body.if_else( cond_outer, outer1_true, @@ -1733,7 +1820,8 @@ def test_break_continue_deeply_nested(self, loop_operation): ) loop_body.if_else(cond_outer, outer2_true, outer2_false, loop_qubits, loop_clbits) loop_body.if_else(cond_outer, outer3_true, outer3_false, loop_qubits, loop_clbits) - loop_body.h(qubits[16]).c_if(clbits[19], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[16]).c_if(clbits[19], 0) expected = QuantumCircuit(qubits, clbits) expected.for_loop(range(2), None, loop_body, loop_qubits, loop_clbits) @@ -1756,33 +1844,41 @@ def test_break_continue_deeply_nested(self, loop_operation): loop_operation(test) # inner true 2 with test.if_test(cond_inner): - test.h(0).c_if(3, 0) - test.h(1).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(4, 0) # outer true 2 with test.if_test(cond_outer): - test.h(2).c_if(5, 0) - test.h(3).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(6, 0) inner_true_body1 = QuantumCircuit(qubits[:4], clbits[:7]) loop_operation(inner_true_body1) inner_true_body2 = QuantumCircuit([qubits[0], clbits[0], clbits[3]]) - inner_true_body2.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + inner_true_body2.h(qubits[0]).c_if(clbits[3], 0) outer_true_body1 = QuantumCircuit(qubits[:4], clbits[:7]) outer_true_body1.if_test(cond_inner, inner_true_body1, qubits[:4], clbits[:7]) outer_true_body1.if_test( cond_inner, inner_true_body2, [qubits[0]], [clbits[0], clbits[3]] ) - outer_true_body1.h(qubits[1]).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + outer_true_body1.h(qubits[1]).c_if(clbits[4], 0) outer_true_body2 = QuantumCircuit([qubits[2], clbits[1], clbits[5]]) - outer_true_body2.h(qubits[2]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + outer_true_body2.h(qubits[2]).c_if(clbits[5], 0) loop_body = QuantumCircuit(qubits[:4], clbits[:7]) loop_body.if_test(cond_outer, outer_true_body1, qubits[:4], clbits[:7]) loop_body.if_test(cond_outer, outer_true_body2, [qubits[2]], [clbits[1], clbits[5]]) - loop_body.h(qubits[3]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[3]).c_if(clbits[6], 0) expected = QuantumCircuit(qubits, clbits) expected.while_loop(cond_loop, loop_body, qubits[:4], clbits[:7]) @@ -1796,31 +1892,43 @@ def test_break_continue_deeply_nested(self, loop_operation): with test.if_test(cond_outer): # inner 1 with test.if_test(cond_inner) as inner1_else: - test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) with inner1_else: - loop_operation(test).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + loop_operation(test).c_if(4, 0) # inner 2 with test.if_test(cond_inner) as inner2_else: - test.h(1).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(5, 0) with inner2_else: - test.h(2).c_if(6, 0) - test.h(3).c_if(7, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(7, 0) # outer 2 with test.if_test(cond_outer) as outer2_else: - test.h(4).c_if(8, 0) + with self.assertWarns(DeprecationWarning): + test.h(4).c_if(8, 0) with outer2_else: - test.h(5).c_if(9, 0) - test.h(6).c_if(10, 0) + with self.assertWarns(DeprecationWarning): + test.h(5).c_if(9, 0) + with self.assertWarns(DeprecationWarning): + test.h(6).c_if(10, 0) inner1_true = QuantumCircuit(qubits[:7], clbits[:11]) - inner1_true.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + inner1_true.h(qubits[0]).c_if(clbits[3], 0) inner1_false = QuantumCircuit(qubits[:7], clbits[:11]) - loop_operation(inner1_false).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + loop_operation(inner1_false).c_if(clbits[4], 0) inner2_true = QuantumCircuit([qubits[1], qubits[2], clbits[0], clbits[5], clbits[6]]) - inner2_true.h(qubits[1]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + inner2_true.h(qubits[1]).c_if(clbits[5], 0) inner2_false = QuantumCircuit([qubits[1], qubits[2], clbits[0], clbits[5], clbits[6]]) - inner2_false.h(qubits[2]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + inner2_false.h(qubits[2]).c_if(clbits[6], 0) outer1_true = QuantumCircuit(qubits[:7], clbits[:11]) outer1_true.if_else(cond_inner, inner1_true, inner1_false, qubits[:7], clbits[:11]) @@ -1831,12 +1939,15 @@ def test_break_continue_deeply_nested(self, loop_operation): qubits[1:3], [clbits[0], clbits[5], clbits[6]], ) - outer1_true.h(qubits[3]).c_if(clbits[7], 0) + with self.assertWarns(DeprecationWarning): + outer1_true.h(qubits[3]).c_if(clbits[7], 0) outer2_true = QuantumCircuit([qubits[4], qubits[5], clbits[1], clbits[8], clbits[9]]) - outer2_true.h(qubits[4]).c_if(clbits[8], 0) + with self.assertWarns(DeprecationWarning): + outer2_true.h(qubits[4]).c_if(clbits[8], 0) outer2_false = QuantumCircuit([qubits[4], qubits[5], clbits[1], clbits[8], clbits[9]]) - outer2_false.h(qubits[5]).c_if(clbits[9], 0) + with self.assertWarns(DeprecationWarning): + outer2_false.h(qubits[5]).c_if(clbits[9], 0) loop_body = QuantumCircuit(qubits[:7], clbits[:11]) loop_body.if_test(cond_outer, outer1_true, qubits[:7], clbits[:11]) @@ -1847,7 +1958,8 @@ def test_break_continue_deeply_nested(self, loop_operation): qubits[4:6], [clbits[1], clbits[8], clbits[9]], ) - loop_body.h(qubits[6]).c_if(clbits[10], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[6]).c_if(clbits[10], 0) expected = QuantumCircuit(qubits, clbits) expected.while_loop(cond_loop, loop_body, qubits[:7], clbits[:11]) @@ -1857,65 +1969,81 @@ def test_break_continue_deeply_nested(self, loop_operation): with self.subTest("while/else/else"): test = QuantumCircuit(qubits, clbits) with test.while_loop(cond_loop): - test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) # outer 1 with test.if_test(cond_outer) as outer1_else: - test.h(1).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(4, 0) with outer1_else: - test.h(2).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(5, 0) # outer 2 (nesting the inner condition in the 'if') with test.if_test(cond_outer) as outer2_else: - test.h(3).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(6, 0) # inner 21 with test.if_test(cond_inner) as inner21_else: loop_operation(test) with inner21_else: - test.h(4).c_if(7, 0) + with self.assertWarns(DeprecationWarning): + test.h(4).c_if(7, 0) # inner 22 with test.if_test(cond_inner) as inner22_else: - test.h(5).c_if(8, 0) + with self.assertWarns(DeprecationWarning): + test.h(5).c_if(8, 0) with inner22_else: loop_operation(test) - test.h(6).c_if(9, 0) + with self.assertWarns(DeprecationWarning): + test.h(6).c_if(9, 0) with outer2_else: - test.h(7).c_if(10, 0) + with self.assertWarns(DeprecationWarning): + test.h(7).c_if(10, 0) # inner 23 with test.if_test(cond_inner) as inner23_else: - test.h(8).c_if(11, 0) + with self.assertWarns(DeprecationWarning): + test.h(8).c_if(11, 0) with inner23_else: - test.h(9).c_if(12, 0) + with self.assertWarns(DeprecationWarning): + test.h(9).c_if(12, 0) # outer 3 (nesting the inner condition in an 'else' branch) with test.if_test(cond_outer) as outer3_else: - test.h(10).c_if(13, 0) + with self.assertWarns(DeprecationWarning): + test.h(10).c_if(13, 0) with outer3_else: - test.h(11).c_if(14, 0) + with self.assertWarns(DeprecationWarning): + test.h(11).c_if(14, 0) # inner 31 with test.if_test(cond_inner) as inner31_else: loop_operation(test) with inner31_else: - test.h(12).c_if(15, 0) + with self.assertWarns(DeprecationWarning): + test.h(12).c_if(15, 0) # inner 32 with test.if_test(cond_inner) as inner32_else: - test.h(13).c_if(16, 0) + with self.assertWarns(DeprecationWarning): + test.h(13).c_if(16, 0) with inner32_else: loop_operation(test) # inner 33 with test.if_test(cond_inner) as inner33_else: - test.h(14).c_if(17, 0) + with self.assertWarns(DeprecationWarning): + test.h(14).c_if(17, 0) with inner33_else: - test.h(15).c_if(18, 0) - - test.h(16).c_if(19, 0) + with self.assertWarns(DeprecationWarning): + test.h(15).c_if(18, 0) + with self.assertWarns(DeprecationWarning): + test.h(16).c_if(19, 0) # End of test "for" loop. # No `clbits[2]` here because that's only used in `cond_loop`, for while loops. @@ -1924,32 +2052,41 @@ def test_break_continue_deeply_nested(self, loop_operation): loop_bits = loop_qubits + loop_clbits outer1_true = QuantumCircuit([qubits[1], qubits[2], clbits[1], clbits[4], clbits[5]]) - outer1_true.h(qubits[1]).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + outer1_true.h(qubits[1]).c_if(clbits[4], 0) outer1_false = QuantumCircuit([qubits[1], qubits[2], clbits[1], clbits[4], clbits[5]]) - outer1_false.h(qubits[2]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + outer1_false.h(qubits[2]).c_if(clbits[5], 0) inner21_true = QuantumCircuit(loop_bits) loop_operation(inner21_true) inner21_false = QuantumCircuit(loop_bits) - inner21_false.h(qubits[4]).c_if(clbits[7], 0) + with self.assertWarns(DeprecationWarning): + inner21_false.h(qubits[4]).c_if(clbits[7], 0) inner22_true = QuantumCircuit(loop_bits) - inner22_true.h(qubits[5]).c_if(clbits[8], 0) + with self.assertWarns(DeprecationWarning): + inner22_true.h(qubits[5]).c_if(clbits[8], 0) inner22_false = QuantumCircuit(loop_bits) loop_operation(inner22_false) inner23_true = QuantumCircuit(qubits[8:10], [clbits[0], clbits[11], clbits[12]]) - inner23_true.h(qubits[8]).c_if(clbits[11], 0) + with self.assertWarns(DeprecationWarning): + inner23_true.h(qubits[8]).c_if(clbits[11], 0) inner23_false = QuantumCircuit(qubits[8:10], [clbits[0], clbits[11], clbits[12]]) - inner23_false.h(qubits[9]).c_if(clbits[12], 0) + with self.assertWarns(DeprecationWarning): + inner23_false.h(qubits[9]).c_if(clbits[12], 0) outer2_true = QuantumCircuit(loop_bits) - outer2_true.h(qubits[3]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + outer2_true.h(qubits[3]).c_if(clbits[6], 0) outer2_true.if_else(cond_inner, inner21_true, inner21_false, loop_qubits, loop_clbits) outer2_true.if_else(cond_inner, inner22_true, inner22_false, loop_qubits, loop_clbits) - outer2_true.h(qubits[6]).c_if(clbits[9], 0) + with self.assertWarns(DeprecationWarning): + outer2_true.h(qubits[6]).c_if(clbits[9], 0) outer2_false = QuantumCircuit(loop_bits) - outer2_false.h(qubits[7]).c_if(clbits[10], 0) + with self.assertWarns(DeprecationWarning): + outer2_false.h(qubits[7]).c_if(clbits[10], 0) outer2_false.if_else( cond_inner, inner23_true, @@ -1961,22 +2098,28 @@ def test_break_continue_deeply_nested(self, loop_operation): inner31_true = QuantumCircuit(loop_bits) loop_operation(inner31_true) inner31_false = QuantumCircuit(loop_bits) - inner31_false.h(qubits[12]).c_if(clbits[15], 0) + with self.assertWarns(DeprecationWarning): + inner31_false.h(qubits[12]).c_if(clbits[15], 0) inner32_true = QuantumCircuit(loop_bits) - inner32_true.h(qubits[13]).c_if(clbits[16], 0) + with self.assertWarns(DeprecationWarning): + inner32_true.h(qubits[13]).c_if(clbits[16], 0) inner32_false = QuantumCircuit(loop_bits) loop_operation(inner32_false) inner33_true = QuantumCircuit(qubits[14:16], [clbits[0], clbits[17], clbits[18]]) - inner33_true.h(qubits[14]).c_if(clbits[17], 0) + with self.assertWarns(DeprecationWarning): + inner33_true.h(qubits[14]).c_if(clbits[17], 0) inner33_false = QuantumCircuit(qubits[14:16], [clbits[0], clbits[17], clbits[18]]) - inner33_false.h(qubits[15]).c_if(clbits[18], 0) + with self.assertWarns(DeprecationWarning): + inner33_false.h(qubits[15]).c_if(clbits[18], 0) outer3_true = QuantumCircuit(loop_bits) - outer3_true.h(qubits[10]).c_if(clbits[13], 0) + with self.assertWarns(DeprecationWarning): + outer3_true.h(qubits[10]).c_if(clbits[13], 0) outer3_false = QuantumCircuit(loop_bits) - outer3_false.h(qubits[11]).c_if(clbits[14], 0) + with self.assertWarns(DeprecationWarning): + outer3_false.h(qubits[11]).c_if(clbits[14], 0) outer3_false.if_else(cond_inner, inner31_true, inner31_false, loop_qubits, loop_clbits) outer3_false.if_else(cond_inner, inner32_true, inner32_false, loop_qubits, loop_clbits) outer3_false.if_else( @@ -1988,7 +2131,8 @@ def test_break_continue_deeply_nested(self, loop_operation): ) loop_body = QuantumCircuit(loop_bits) - loop_body.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[0]).c_if(clbits[3], 0) loop_body.if_else( cond_outer, outer1_true, @@ -1998,7 +2142,8 @@ def test_break_continue_deeply_nested(self, loop_operation): ) loop_body.if_else(cond_outer, outer2_true, outer2_false, loop_qubits, loop_clbits) loop_body.if_else(cond_outer, outer3_true, outer3_false, loop_qubits, loop_clbits) - loop_body.h(qubits[16]).c_if(clbits[19], 0) + with self.assertWarns(DeprecationWarning): + loop_body.h(qubits[16]).c_if(clbits[19], 0) expected = QuantumCircuit(qubits, clbits) expected.while_loop(cond_loop, loop_body, loop_qubits, loop_clbits) @@ -2008,52 +2153,68 @@ def test_break_continue_deeply_nested(self, loop_operation): with self.subTest("if/while/if/switch"): test = QuantumCircuit(qubits, clbits) with test.if_test(cond_outer): # outer_t - test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) with test.while_loop(cond_loop): # loop - test.h(1).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(4, 0) with test.if_test(cond_inner): # inner_t - test.h(2).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(5, 0) with test.switch(5) as case_: with case_(False): # case_f - test.h(3).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(6, 0) with case_(True): # case_t loop_operation(test) - test.h(4).c_if(7, 0) + with self.assertWarns(DeprecationWarning): + test.h(4).c_if(7, 0) # exit inner_t - test.h(5).c_if(8, 0) + with self.assertWarns(DeprecationWarning): + test.h(5).c_if(8, 0) # exit loop - test.h(6).c_if(9, 0) + with self.assertWarns(DeprecationWarning): + test.h(6).c_if(9, 0) # exit outer_t - test.h(7).c_if(10, 0) + with self.assertWarns(DeprecationWarning): + test.h(7).c_if(10, 0) case_f = QuantumCircuit(qubits[1:6], [clbits[0], clbits[2]] + clbits[4:9]) - case_f.h(qubits[3]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + case_f.h(qubits[3]).c_if(clbits[6], 0) case_t = QuantumCircuit(qubits[1:6], [clbits[0], clbits[2]] + clbits[4:9]) loop_operation(case_t) inner_t = QuantumCircuit(qubits[1:6], [clbits[0], clbits[2]] + clbits[4:9]) - inner_t.h(qubits[2]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + inner_t.h(qubits[2]).c_if(clbits[5], 0) inner_t.switch( clbits[5], [(False, case_f), (True, case_t)], qubits[1:6], [clbits[0], clbits[2]] + clbits[4:9], ) - inner_t.h(qubits[4]).c_if(clbits[7], 0) + with self.assertWarns(DeprecationWarning): + inner_t.h(qubits[4]).c_if(clbits[7], 0) loop = QuantumCircuit(qubits[1:6], [clbits[0], clbits[2]] + clbits[4:9]) - loop.h(qubits[1]).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + loop.h(qubits[1]).c_if(clbits[4], 0) loop.if_test(cond_inner, inner_t, qubits[1:6], [clbits[0], clbits[2]] + clbits[4:9]) - loop.h(qubits[5]).c_if(clbits[8], 0) + with self.assertWarns(DeprecationWarning): + loop.h(qubits[5]).c_if(clbits[8], 0) outer_t = QuantumCircuit(qubits[:7], clbits[:10]) - outer_t.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + outer_t.h(qubits[0]).c_if(clbits[3], 0) outer_t.while_loop(cond_loop, loop, qubits[1:6], [clbits[0], clbits[2]] + clbits[4:9]) - outer_t.h(qubits[6]).c_if(clbits[9], 0) + with self.assertWarns(DeprecationWarning): + outer_t.h(qubits[6]).c_if(clbits[9], 0) expected = QuantumCircuit(qubits, clbits) expected.if_test(cond_outer, outer_t, qubits[:7], clbits[:10]) - expected.h(qubits[7]).c_if(clbits[10], 0) + with self.assertWarns(DeprecationWarning): + expected.h(qubits[7]).c_if(clbits[10], 0) self.assertEqual(canonicalize_control_flow(test), canonicalize_control_flow(expected)) @@ -2061,64 +2222,82 @@ def test_break_continue_deeply_nested(self, loop_operation): test = QuantumCircuit(qubits, clbits) with test.switch(0) as case_outer: with case_outer(False): # outer_case_f - test.h(0).c_if(3, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(3, 0) with test.for_loop(range(2)): # loop - test.h(1).c_if(4, 0) + with self.assertWarns(DeprecationWarning): + test.h(1).c_if(4, 0) with test.switch(1) as case_inner: with case_inner(False): # inner_case_f - test.h(2).c_if(5, 0) + with self.assertWarns(DeprecationWarning): + test.h(2).c_if(5, 0) with test.if_test((2, True)) as else_: # if_t - test.h(3).c_if(6, 0) + with self.assertWarns(DeprecationWarning): + test.h(3).c_if(6, 0) with else_: # if_f loop_operation(test) - test.h(4).c_if(7, 0) + with self.assertWarns(DeprecationWarning): + test.h(4).c_if(7, 0) with case_inner(True): # inner_case_t loop_operation(test) - test.h(5).c_if(8, 0) + with self.assertWarns(DeprecationWarning): + test.h(5).c_if(8, 0) # exit loop1 - test.h(6).c_if(9, 0) + with self.assertWarns(DeprecationWarning): + test.h(6).c_if(9, 0) with case_outer(True): # outer_case_t - test.h(7).c_if(10, 0) - test.h(8).c_if(11, 0) + with self.assertWarns(DeprecationWarning): + test.h(7).c_if(10, 0) + with self.assertWarns(DeprecationWarning): + test.h(8).c_if(11, 0) if_t = QuantumCircuit(qubits[1:6], clbits[1:3] + clbits[4:9]) - if_t.h(qubits[3]).c_if(clbits[6], 0) + with self.assertWarns(DeprecationWarning): + if_t.h(qubits[3]).c_if(clbits[6], 0) if_f = QuantumCircuit(qubits[1:6], clbits[1:3] + clbits[4:9]) loop_operation(if_f) inner_case_f = QuantumCircuit(qubits[1:6], clbits[1:3] + clbits[4:9]) - inner_case_f.h(qubits[2]).c_if(clbits[5], 0) + with self.assertWarns(DeprecationWarning): + inner_case_f.h(qubits[2]).c_if(clbits[5], 0) inner_case_f.if_else( (clbits[2], True), if_t, if_f, qubits[1:6], clbits[1:3] + clbits[4:9] ) - inner_case_f.h(qubits[4]).c_if(clbits[7], 0) + with self.assertWarns(DeprecationWarning): + inner_case_f.h(qubits[4]).c_if(clbits[7], 0) inner_case_t = QuantumCircuit(qubits[1:6], clbits[1:3] + clbits[4:9]) loop_operation(inner_case_t) loop = QuantumCircuit(qubits[1:6], clbits[1:3] + clbits[4:9]) - loop.h(qubits[1]).c_if(clbits[4], 0) + with self.assertWarns(DeprecationWarning): + loop.h(qubits[1]).c_if(clbits[4], 0) loop.switch( clbits[1], [(False, inner_case_f), (True, inner_case_t)], qubits[1:6], clbits[1:3] + clbits[4:9], ) - loop.h(qubits[5]).c_if(clbits[8], 0) + with self.assertWarns(DeprecationWarning): + loop.h(qubits[5]).c_if(clbits[8], 0) outer_case_f = QuantumCircuit(qubits[:8], clbits[:11]) - outer_case_f.h(qubits[0]).c_if(clbits[3], 0) + with self.assertWarns(DeprecationWarning): + outer_case_f.h(qubits[0]).c_if(clbits[3], 0) outer_case_f.for_loop(range(2), None, loop, qubits[1:6], clbits[1:3] + clbits[4:9]) - outer_case_f.h(qubits[6]).c_if(clbits[9], 0) + with self.assertWarns(DeprecationWarning): + outer_case_f.h(qubits[6]).c_if(clbits[9], 0) outer_case_t = QuantumCircuit(qubits[:8], clbits[:11]) - outer_case_t.h(qubits[7]).c_if(clbits[10], 0) + with self.assertWarns(DeprecationWarning): + outer_case_t.h(qubits[7]).c_if(clbits[10], 0) expected = QuantumCircuit(qubits, clbits) expected.switch( clbits[0], [(False, outer_case_f), (True, outer_case_t)], qubits[:8], clbits[:11] ) - expected.h(qubits[8]).c_if(clbits[11], 0) + with self.assertWarns(DeprecationWarning): + expected.h(qubits[8]).c_if(clbits[11], 0) self.assertEqual(canonicalize_control_flow(test), canonicalize_control_flow(expected)) @@ -2351,10 +2530,12 @@ def test_access_of_clbit_from_c_if(self): with self.subTest("if"): test = QuantumCircuit(bits) with test.if_test(cond): - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) body = QuantumCircuit([qubits[0]], clbits) - body.h(qubits[0]).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(clbits[1], 0) expected = QuantumCircuit(bits) expected.if_test(cond, body, [qubits[0]], clbits) @@ -2363,41 +2544,49 @@ def test_access_of_clbit_from_c_if(self): with test.if_test(cond) as else_: pass with else_: - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) true_body = QuantumCircuit([qubits[0]], clbits) false_body = QuantumCircuit([qubits[0]], clbits) - false_body.h(qubits[0]).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + false_body.h(qubits[0]).c_if(clbits[1], 0) expected = QuantumCircuit(bits) expected.if_else(cond, true_body, false_body, [qubits[0]], clbits) with self.subTest("for"): test = QuantumCircuit(bits) with test.for_loop(range(2)): - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) body = QuantumCircuit([qubits[0]], clbits) - body.h(qubits[0]).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(clbits[1], 0) expected = QuantumCircuit(bits) expected.for_loop(range(2), None, body, [qubits[0]], clbits) with self.subTest("while"): test = QuantumCircuit(bits) with test.while_loop(cond): - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) body = QuantumCircuit([qubits[0]], clbits) - body.h(qubits[0]).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(clbits[1], 0) expected = QuantumCircuit(bits) expected.while_loop(cond, body, [qubits[0]], clbits) with self.subTest("switch"): test = QuantumCircuit(bits) with test.switch(cond[0]) as case, case(False): - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) body = QuantumCircuit([qubits[0]], clbits) - body.h(qubits[0]).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(clbits[1], 0) expected = QuantumCircuit(bits) expected.switch(cond[0], [(False, body)], [qubits[0]], clbits) @@ -2405,10 +2594,12 @@ def test_access_of_clbit_from_c_if(self): test = QuantumCircuit(bits) with test.for_loop(range(2)): with test.if_test(cond): - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) true_body = QuantumCircuit([qubits[0]], clbits) - true_body.h(qubits[0]).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + true_body.h(qubits[0]).c_if(clbits[1], 0) body = QuantumCircuit([qubits[0]], clbits) body.if_test(cond, body, [qubits[0]], clbits) expected = QuantumCircuit(bits) @@ -2417,10 +2608,12 @@ def test_access_of_clbit_from_c_if(self): with self.subTest("switch inside for"): test = QuantumCircuit(bits) with test.for_loop(range(2)), test.switch(cond[0]) as case, case(False): - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) body = QuantumCircuit([qubits[0]], clbits) - body.h(qubits[0]).c_if(clbits[1], 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(clbits[1], 0) body = QuantumCircuit([qubits[0]], clbits) body.switch(cond[0], [(False, body)], [qubits[0]], clbits) expected = QuantumCircuit(bits) @@ -2438,10 +2631,12 @@ def test_access_of_classicalregister_from_c_if(self): with self.subTest("if"): test = QuantumCircuit(qubits, clbits, creg) with test.if_test(cond): - test.h(0).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(creg, 0) body = QuantumCircuit([qubits[0]], clbits, creg) - body.h(qubits[0]).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(creg, 0) expected = QuantumCircuit(qubits, clbits, creg) expected.if_test(cond, body, [qubits[0]], all_clbits) @@ -2450,41 +2645,49 @@ def test_access_of_classicalregister_from_c_if(self): with test.if_test(cond) as else_: pass with else_: - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) true_body = QuantumCircuit([qubits[0]], clbits, creg) false_body = QuantumCircuit([qubits[0]], clbits, creg) - false_body.h(qubits[0]).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + false_body.h(qubits[0]).c_if(creg, 0) expected = QuantumCircuit(qubits, clbits, creg) expected.if_else(cond, true_body, false_body, [qubits[0]], all_clbits) with self.subTest("for"): test = QuantumCircuit(qubits, clbits, creg) with test.for_loop(range(2)): - test.h(0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(1, 0) body = QuantumCircuit([qubits[0]], clbits, creg) - body.h(qubits[0]).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(creg, 0) expected = QuantumCircuit(qubits, clbits, creg) expected.for_loop(range(2), None, body, [qubits[0]], all_clbits) with self.subTest("while"): test = QuantumCircuit(qubits, clbits, creg) with test.while_loop(cond): - test.h(0).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(creg, 0) body = QuantumCircuit([qubits[0]], clbits, creg) - body.h(qubits[0]).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(creg, 0) expected = QuantumCircuit(qubits, clbits, creg) expected.while_loop(cond, body, [qubits[0]], all_clbits) with self.subTest("switch"): test = QuantumCircuit(qubits, clbits, creg) with test.switch(cond[0]) as case, case(False): - test.h(0).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(creg, 0) body = QuantumCircuit([qubits[0]], clbits, creg) - body.h(qubits[0]).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + body.h(qubits[0]).c_if(creg, 0) expected = QuantumCircuit(qubits, clbits, creg) expected.switch(cond[0], [(False, body)], [qubits[0]], all_clbits) @@ -2492,10 +2695,12 @@ def test_access_of_classicalregister_from_c_if(self): test = QuantumCircuit(qubits, clbits, creg) with test.for_loop(range(2)): with test.if_test(cond): - test.h(0).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(creg, 0) true_body = QuantumCircuit([qubits[0]], clbits, creg) - true_body.h(qubits[0]).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + true_body.h(qubits[0]).c_if(creg, 0) body = QuantumCircuit([qubits[0]], clbits, creg) body.if_test(cond, body, [qubits[0]], all_clbits) expected = QuantumCircuit(qubits, clbits, creg) @@ -2505,10 +2710,12 @@ def test_access_of_classicalregister_from_c_if(self): test = QuantumCircuit(qubits, clbits, creg) with test.for_loop(range(2)): with test.switch(cond[0]) as case, case(False): - test.h(0).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + test.h(0).c_if(creg, 0) case = QuantumCircuit([qubits[0]], clbits, creg) - case.h(qubits[0]).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + case.h(qubits[0]).c_if(creg, 0) body = QuantumCircuit([qubits[0]], clbits, creg) body.switch(cond[0], [(False, case)], [qubits[0]], all_clbits) expected = QuantumCircuit(qubits, clbits, creg) @@ -3424,7 +3631,8 @@ def test_if_placeholder_rejects_c_if(self): NotImplementedError, r"IfElseOp cannot be classically controlled through Instruction\.c_if", ): - placeholder.c_if(bits[1], 0) + with self.assertWarns(DeprecationWarning): + placeholder.c_if(bits[1], 0) with self.subTest("else"): test = QuantumCircuit(bits) @@ -3441,7 +3649,8 @@ def test_if_placeholder_rejects_c_if(self): NotImplementedError, r"IfElseOp cannot be classically controlled through Instruction\.c_if", ): - placeholder.c_if(bits[1], 0) + with self.assertWarns(DeprecationWarning): + placeholder.c_if(bits[1], 0) def test_switch_rejects_operations_outside_cases(self): """It shouldn't be permissible to try and put instructions inside a switch but outside a @@ -3543,7 +3752,8 @@ def test_reject_c_if_from_outside_scope(self): with self.assertRaisesRegex( CircuitError, r"Cannot add resources after the scope has been built\." ): - instructions.c_if(*cond) + with self.assertWarns(DeprecationWarning): + instructions.c_if(*cond) with self.subTest("else"): test = QuantumCircuit(bits) @@ -3554,7 +3764,8 @@ def test_reject_c_if_from_outside_scope(self): with self.assertRaisesRegex( CircuitError, r"Cannot add resources after the scope has been built\." ): - instructions.c_if(*cond) + with self.assertWarns(DeprecationWarning): + instructions.c_if(*cond) with self.subTest("for"): test = QuantumCircuit(bits) @@ -3563,7 +3774,8 @@ def test_reject_c_if_from_outside_scope(self): with self.assertRaisesRegex( CircuitError, r"Cannot add resources after the scope has been built\." ): - instructions.c_if(*cond) + with self.assertWarns(DeprecationWarning): + instructions.c_if(*cond) with self.subTest("while"): test = QuantumCircuit(bits) @@ -3572,7 +3784,8 @@ def test_reject_c_if_from_outside_scope(self): with self.assertRaisesRegex( CircuitError, r"Cannot add resources after the scope has been built\." ): - instructions.c_if(*cond) + with self.assertWarns(DeprecationWarning): + instructions.c_if(*cond) with self.subTest("switch"): test = QuantumCircuit(bits) @@ -3581,7 +3794,8 @@ def test_reject_c_if_from_outside_scope(self): with self.assertRaisesRegex( CircuitError, r"Cannot add resources after the scope has been built\." ): - instructions.c_if(*cond) + with self.assertWarns(DeprecationWarning): + instructions.c_if(*cond) with self.subTest("if inside for"): # As a side-effect of how the lazy building of 'if' statements works, we actually @@ -3595,7 +3809,8 @@ def test_reject_c_if_from_outside_scope(self): with self.assertRaisesRegex( CircuitError, r"Cannot add resources after the scope has been built\." ): - instructions.c_if(*cond) + with self.assertWarns(DeprecationWarning): + instructions.c_if(*cond) with self.subTest("switch inside for"): # `switch` has the same lazy building as `if`, so is subject to the same considerations @@ -3608,7 +3823,8 @@ def test_reject_c_if_from_outside_scope(self): with self.assertRaisesRegex( CircuitError, r"Cannot add resources after the scope has been built\." ): - instructions.c_if(*cond) + with self.assertWarns(DeprecationWarning): + instructions.c_if(*cond) def test_raising_inside_context_manager_leave_circuit_usable(self): """Test that if we leave a builder by raising some sort of exception, the circuit is left in diff --git a/test/python/circuit/test_extensions_standard.py b/test/python/circuit/test_extensions_standard.py index 73e00fa84c90..042087baa6d9 100644 --- a/test/python/circuit/test_extensions_standard.py +++ b/test/python/circuit/test_extensions_standard.py @@ -76,7 +76,8 @@ def test_barrier_invalid(self): def test_conditional_barrier_invalid(self): qc = self.circuit barrier = qc.barrier(self.qr) - self.assertRaises(QiskitError, barrier.c_if, self.cr, 0) + with self.assertWarns(DeprecationWarning): + self.assertRaises(QiskitError, barrier.c_if, self.cr, 0) def test_barrier_reg(self): self.circuit.barrier(self.qr) @@ -131,13 +132,15 @@ def test_ch_invalid(self): self.assertRaises(CircuitError, qc.ch, "a", self.qr[1]) def test_cif_reg(self): - self.circuit.h(self.qr[0]).c_if(self.cr, 7) + with self.assertWarns(DeprecationWarning): + self.circuit.h(self.qr[0]).c_if(self.cr, 7) self.assertEqual(self.circuit[0].operation.name, "h") self.assertEqual(self.circuit[0].qubits, (self.qr[0],)) self.assertEqual(self.circuit[0].operation.condition, (self.cr, 7)) def test_cif_single_bit(self): - self.circuit.h(self.qr[0]).c_if(self.cr[0], True) + with self.assertWarns(DeprecationWarning): + self.circuit.h(self.qr[0]).c_if(self.cr[0], True) self.assertEqual(self.circuit[0].operation.name, "h") self.assertEqual(self.circuit[0].qubits, (self.qr[0],)) self.assertEqual(self.circuit[0].operation.condition, (self.cr[0], True)) diff --git a/test/python/circuit/test_instruction_repeat.py b/test/python/circuit/test_instruction_repeat.py index 778e6aad64c2..74142fa898e9 100644 --- a/test/python/circuit/test_instruction_repeat.py +++ b/test/python/circuit/test_instruction_repeat.py @@ -55,7 +55,8 @@ def test_standard_1Q_one(self): def test_conditional(self): """Test that repetition works with a condition.""" cr = ClassicalRegister(3, "cr") - gate = SGate().c_if(cr, 7).repeat(5) + with self.assertWarns(DeprecationWarning): + gate = SGate().c_if(cr, 7).repeat(5) self.assertEqual(gate.condition, (cr, 7)) defn = QuantumCircuit(1) @@ -98,7 +99,8 @@ def test_standard_2Q_one(self): def test_conditional(self): """Test that repetition works with a condition.""" cr = ClassicalRegister(3, "cr") - gate = CXGate().c_if(cr, 7).repeat(5) + with self.assertWarns(DeprecationWarning): + gate = CXGate().c_if(cr, 7).repeat(5) self.assertEqual(gate.condition, (cr, 7)) defn = QuantumCircuit(2) @@ -145,7 +147,8 @@ def test_measure_one(self): def test_measure_conditional(self): """Test conditional measure moves condition to the outside.""" cr = ClassicalRegister(3, "cr") - measure = Measure().c_if(cr, 7).repeat(5) + with self.assertWarns(DeprecationWarning): + measure = Measure().c_if(cr, 7).repeat(5) self.assertEqual(measure.condition, (cr, 7)) defn = QuantumCircuit(1, 1) diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index 170b47632c4d..a412d09a2302 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -176,7 +176,8 @@ def circuit_instruction_circuit_roundtrip(self): circ1.u(0.1, 0.2, -0.2, q[0]) circ1.barrier() circ1.measure(q, c) - circ1.rz(0.8, q[0]).c_if(c, 6) + with self.assertWarns(DeprecationWarning): + circ1.rz(0.8, q[0]).c_if(c, 6) inst = circ1.to_instruction() circ2 = QuantumCircuit(q, c, name="circ2") @@ -238,16 +239,20 @@ def test_reverse_instruction(self): circ.u(0.1, 0.2, -0.2, q[0]) circ.barrier() circ.measure(q[0], c[0]) - circ.rz(0.8, q[0]).c_if(c, 6) - inst = circ.to_instruction() + with self.assertWarns(DeprecationWarning): + circ.rz(0.8, q[0]).c_if(c, 6) + with self.assertWarns(DeprecationWarning): + inst = circ.to_instruction() circ = QuantumCircuit(q, c, name="circ") - circ.rz(0.8, q[0]).c_if(c, 6) + with self.assertWarns(DeprecationWarning): + circ.rz(0.8, q[0]).c_if(c, 6) circ.measure(q[0], c[0]) circ.barrier() circ.u(0.1, 0.2, -0.2, q[0]) circ.t(q[1]) - inst_reverse = circ.to_instruction() + with self.assertWarns(DeprecationWarning): + inst_reverse = circ.to_instruction() self.assertEqual(inst.reverse_ops().definition, inst_reverse.definition) @@ -336,8 +341,10 @@ def test_inverse_instruction_with_conditional(self): circ.u(0.1, 0.2, -0.2, q[0]) circ.barrier() circ.measure(q[0], c[0]) - circ.rz(0.8, q[0]).c_if(c, 6) - inst = circ.to_instruction() + with self.assertWarns(DeprecationWarning): + circ.rz(0.8, q[0]).c_if(c, 6) + with self.assertWarns(DeprecationWarning): + inst = circ.to_instruction() self.assertRaises(CircuitError, inst.inverse) def test_inverse_opaque(self): @@ -467,7 +474,8 @@ def test_instructionset_c_if_direct_resource(self): def case(resource): qc = QuantumCircuit(cr1, qubits, loose_clbits, cr2, cr3) - qc.x(0).c_if(resource, 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(resource, 0) c_if_resource = qc.data[0].operation.condition[0] self.assertIs(c_if_resource, resource) @@ -500,7 +508,8 @@ def test_instructionset_c_if_indexing(self): qc = QuantumCircuit(cr1, qubits, loose_clbits, cr2, cr3) for index, clbit in enumerate(qc.clbits): with self.subTest(index=index): - qc.x(0).c_if(index, 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(index, 0) qc.measure(0, index) from_c_if = qc.data[-2].operation.condition[0] from_measure = qc.data[-1].clbits[0] @@ -516,13 +525,16 @@ def test_instructionset_c_if_size_1_classical_register(self): qc = QuantumCircuit(qr, cr) with self.subTest("classical register"): - qc.x(0).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(cr, 0) self.assertIs(qc.data[-1].operation.condition[0], cr) with self.subTest("classical bit by value"): - qc.x(0).c_if(cr[0], 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(cr[0], 0) self.assertIs(qc.data[-1].operation.condition[0], cr[0]) with self.subTest("classical bit by index"): - qc.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 0) self.assertIs(qc.data[-1].operation.condition[0], cr[0]) def test_instructionset_c_if_no_classical_registers(self): @@ -533,10 +545,12 @@ def test_instructionset_c_if_no_classical_registers(self): bits = [Qubit(), Clbit()] qc = QuantumCircuit(bits) with self.subTest("by value"): - qc.x(0).c_if(bits[1], 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(bits[1], 0) self.assertIs(qc.data[-1].operation.condition[0], bits[1]) with self.subTest("by index"): - qc.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 0) self.assertIs(qc.data[-1].operation.condition[0], bits[1]) def test_instructionset_c_if_rejects_invalid_specifiers(self): @@ -550,7 +564,8 @@ def case(specifier, message): qc = QuantumCircuit(qreg, creg) instruction = qc.x(0) with self.assertRaisesRegex(CircuitError, message): - instruction.c_if(specifier, 0) + with self.assertWarns(DeprecationWarning): + instruction.c_if(specifier, 0) with self.subTest("absent bit"): case(Clbit(), r"Clbit .* is not present in this circuit\.") @@ -574,21 +589,24 @@ def test_instructionset_c_if_with_no_requester(self): instructions = InstructionSet() instructions.add(instruction, [Qubit()], []) register = ClassicalRegister(2) - instructions.c_if(register, 0) + with self.assertWarns(DeprecationWarning): + instructions.c_if(register, 0) self.assertIs(instructions[0].operation.condition[0], register) with self.subTest("accepts arbitrary bit"): instruction = RZGate(0) instructions = InstructionSet() instructions.add(instruction, [Qubit()], []) bit = Clbit() - instructions.c_if(bit, 0) + with self.assertWarns(DeprecationWarning): + instructions.c_if(bit, 0) self.assertIs(instructions[0].operation.condition[0], bit) with self.subTest("rejects index"): instruction = RZGate(0) instructions = InstructionSet() instructions.add(instruction, [Qubit()], []) with self.assertRaisesRegex(CircuitError, r"Cannot pass an index as a condition .*"): - instructions.c_if(0, 0) + with self.assertWarns(DeprecationWarning): + instructions.c_if(0, 0) def test_instructionset_c_if_calls_custom_requester(self): """Test that :meth:`.InstructionSet.c_if` calls a custom requester, and uses its output.""" @@ -613,7 +631,8 @@ def dummy_requester(specifier): instructions = InstructionSet(resource_requester=dummy_requester) instructions.add(instruction, [Qubit()], []) bit = Clbit() - instructions.c_if(bit, 0) + with self.assertWarns(DeprecationWarning): + instructions.c_if(bit, 0) dummy_requester.assert_called_once_with(bit) self.assertIs(instructions[0].operation.condition[0], sentinel_bit) with self.subTest("calls requester with index"): @@ -622,7 +641,8 @@ def dummy_requester(specifier): instructions = InstructionSet(resource_requester=dummy_requester) instructions.add(instruction, [Qubit()], []) index = 0 - instructions.c_if(index, 0) + with self.assertWarns(DeprecationWarning): + instructions.c_if(index, 0) dummy_requester.assert_called_once_with(index) self.assertIs(instructions[0].operation.condition[0], sentinel_bit) with self.subTest("calls requester with register"): @@ -631,7 +651,8 @@ def dummy_requester(specifier): instructions = InstructionSet(resource_requester=dummy_requester) instructions.add(instruction, [Qubit()], []) register = ClassicalRegister(2) - instructions.c_if(register, 0) + with self.assertWarns(DeprecationWarning): + instructions.c_if(register, 0) dummy_requester.assert_called_once_with(register) self.assertIs(instructions[0].operation.condition[0], sentinel_register) with self.subTest("calls requester only once when broadcast"): @@ -641,7 +662,8 @@ def dummy_requester(specifier): for instruction in instruction_list: instructions.add(instruction, [Qubit()], []) register = ClassicalRegister(2) - instructions.c_if(register, 0) + with self.assertWarns(DeprecationWarning): + instructions.c_if(register, 0) dummy_requester.assert_called_once_with(register) for instruction in instruction_list: self.assertIs(instructions[0].operation.condition[0], sentinel_register) diff --git a/test/python/circuit/test_random_circuit.py b/test/python/circuit/test_random_circuit.py index ebbdfd28d648..0d798fe8ad19 100644 --- a/test/python/circuit/test_random_circuit.py +++ b/test/python/circuit/test_random_circuit.py @@ -49,14 +49,16 @@ def test_random_circuit_conditional_reset(self): """Test generating random circuits with conditional and reset.""" num_qubits = 1 depth = 100 - circ = random_circuit(num_qubits, depth, conditional=True, reset=True, seed=5) + with self.assertWarns(DeprecationWarning): + circ = random_circuit(num_qubits, depth, conditional=True, reset=True, seed=5) self.assertEqual(circ.width(), 2 * num_qubits) self.assertIn("reset", circ.count_ops()) def test_large_conditional(self): """Test that conditions do not fail with large conditionals. Regression test of gh-6994.""" # The main test is that this call actually returns without raising an exception. - circ = random_circuit(64, 2, conditional=True, seed=0) + with self.assertWarns(DeprecationWarning): + circ = random_circuit(64, 2, conditional=True, seed=0) # Test that at least one instruction had a condition generated. It's possible that this # fails due to very bad luck with the random seed - if so, change the seed to ensure that a # condition _is_ generated, because we need to test that generation doesn't error. @@ -72,7 +74,8 @@ def test_large_conditional(self): def test_random_mid_circuit_measure_conditional(self): """Test random circuit with mid-circuit measurements for conditionals.""" num_qubits = depth = 2 - circ = random_circuit(num_qubits, depth, conditional=True, seed=16) + with self.assertWarns(DeprecationWarning): + circ = random_circuit(num_qubits, depth, conditional=True, seed=16) self.assertEqual(circ.width(), 2 * num_qubits) op_names = [instruction.operation.name for instruction in circ] # Before a condition, there needs to be measurement in all the qubits. diff --git a/test/python/circuit/test_registerless_circuit.py b/test/python/circuit/test_registerless_circuit.py index 3f77919957da..c3da073e5234 100644 --- a/test/python/circuit/test_registerless_circuit.py +++ b/test/python/circuit/test_registerless_circuit.py @@ -195,10 +195,12 @@ def test_circuit_conditional(self): qreg = QuantumRegister(2) creg = ClassicalRegister(4) circuit = QuantumCircuit(qreg, creg) - circuit.h(0).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + circuit.h(0).c_if(creg, 3) expected = QuantumCircuit(qreg, creg) - expected.h(qreg[0]).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + expected.h(qreg[0]).c_if(creg, 3) self.assertEqual(circuit, expected) @@ -333,11 +335,14 @@ def test_circuit_conditional(self): qreg1 = QuantumRegister(2) creg = ClassicalRegister(2) circuit = QuantumCircuit(qreg0, qreg1, creg) - circuit.h(range(1, 3)).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + circuit.h(range(1, 3)).c_if(creg, 3) expected = QuantumCircuit(qreg0, qreg1, creg) - expected.h(qreg0[1]).c_if(creg, 3) - expected.h(qreg1[0]).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + expected.h(qreg0[1]).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + expected.h(qreg1[0]).c_if(creg, 3) self.assertEqual(circuit, expected) @@ -466,11 +471,14 @@ def test_circuit_conditional(self): qreg1 = QuantumRegister(2) creg = ClassicalRegister(2) circuit = QuantumCircuit(qreg0, qreg1, creg) - circuit.h(slice(1, 3)).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + circuit.h(slice(1, 3)).c_if(creg, 3) expected = QuantumCircuit(qreg0, qreg1, creg) - expected.h(qreg0[1]).c_if(creg, 3) - expected.h(qreg1[0]).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + expected.h(qreg0[1]).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + expected.h(qreg1[0]).c_if(creg, 3) self.assertEqual(circuit, expected) @@ -504,10 +512,12 @@ def test_bit_conditional_single_gate(self): qreg = QuantumRegister(1) creg = ClassicalRegister(2) circuit = QuantumCircuit(qreg, creg) - circuit.h(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + circuit.h(0).c_if(0, True) expected = QuantumCircuit(qreg, creg) - expected.h(qreg[0]).c_if(creg[0], True) + with self.assertWarns(DeprecationWarning): + expected.h(qreg[0]).c_if(creg[0], True) self.assertEqual(circuit, expected) def test_bit_conditional_multiple_gates(self): @@ -516,12 +526,18 @@ def test_bit_conditional_multiple_gates(self): creg = ClassicalRegister(2) creg1 = ClassicalRegister(1) circuit = QuantumCircuit(qreg, creg, creg1) - circuit.h(0).c_if(0, True) - circuit.h(1).c_if(1, False) - circuit.cx(1, 0).c_if(2, True) + with self.assertWarns(DeprecationWarning): + circuit.h(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + circuit.h(1).c_if(1, False) + with self.assertWarns(DeprecationWarning): + circuit.cx(1, 0).c_if(2, True) expected = QuantumCircuit(qreg, creg, creg1) - expected.h(qreg[0]).c_if(creg[0], True) - expected.h(qreg[1]).c_if(creg[1], False) - expected.cx(qreg[1], qreg[0]).c_if(creg1[0], True) + with self.assertWarns(DeprecationWarning): + expected.h(qreg[0]).c_if(creg[0], True) + with self.assertWarns(DeprecationWarning): + expected.h(qreg[1]).c_if(creg[1], False) + with self.assertWarns(DeprecationWarning): + expected.cx(qreg[1], qreg[0]).c_if(creg1[0], True) self.assertEqual(circuit, expected) diff --git a/test/python/circuit/test_singleton.py b/test/python/circuit/test_singleton.py index 40549dd0ad15..8a2b57235f06 100644 --- a/test/python/circuit/test_singleton.py +++ b/test/python/circuit/test_singleton.py @@ -63,7 +63,8 @@ def test_label_not_singleton(self): def test_condition_not_singleton(self): gate = HGate() - condition_gate = HGate().c_if(Clbit(), 0) + with self.assertWarns(DeprecationWarning): + condition_gate = HGate().c_if(Clbit(), 0) self.assertIsNot(gate, condition_gate) def test_raise_on_state_mutation(self): @@ -76,7 +77,8 @@ def test_raise_on_state_mutation(self): def test_labeled_condition(self): singleton_gate = HGate() clbit = Clbit() - gate = HGate(label="conditionally special").c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + gate = HGate(label="conditionally special").c_if(clbit, 0) self.assertIsNot(singleton_gate, gate) self.assertEqual(gate.label, "conditionally special") self.assertEqual(gate.condition, (clbit, 0)) @@ -109,14 +111,16 @@ def test_label_copy_new(self): self.assertEqual(copied_label.label, "special") def test_condition_copy(self): - gate = HGate().c_if(Clbit(), 0) + with self.assertWarns(DeprecationWarning): + gate = HGate().c_if(Clbit(), 0) copied = gate.copy() self.assertIsNot(gate, copied) self.assertEqual(gate, copied) def test_condition_label_copy(self): clbit = Clbit() - gate = HGate(label="conditionally special").c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + gate = HGate(label="conditionally special").c_if(clbit, 0) copied = gate.copy() self.assertIsNot(gate, copied) self.assertEqual(gate, copied) @@ -136,14 +140,16 @@ def test_deepcopy_with_label(self): self.assertEqual(copied.label, "special") def test_deepcopy_with_condition(self): - gate = HGate().c_if(Clbit(), 0) + with self.assertWarns(DeprecationWarning): + gate = HGate().c_if(Clbit(), 0) copied = copy.deepcopy(gate) self.assertIsNot(gate, copied) self.assertEqual(gate, copied) def test_condition_label_deepcopy(self): clbit = Clbit() - gate = HGate(label="conditionally special").c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + gate = HGate(label="conditionally special").c_if(clbit, 0) copied = copy.deepcopy(gate) self.assertIsNot(gate, copied) self.assertEqual(gate, copied) @@ -193,7 +199,8 @@ def test_round_trip_dag_conversion_with_label(self): def test_round_trip_dag_conversion_with_condition(self): qc = QuantumCircuit(1, 1) - gate = HGate().c_if(qc.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + gate = HGate().c_if(qc.cregs[0], 0) qc.append(gate, [0]) dag = circuit_to_dag(qc) out = dag_to_circuit(dag) @@ -203,7 +210,8 @@ def test_round_trip_dag_conversion_with_condition(self): def test_round_trip_dag_conversion_condition_label(self): qc = QuantumCircuit(1, 1) - gate = HGate(label="conditionally special").c_if(qc.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + gate = HGate(label="conditionally special").c_if(qc.cregs[0], 0) qc.append(gate, [0]) dag = circuit_to_dag(qc) out = dag_to_circuit(dag) @@ -217,7 +225,8 @@ def test_condition_via_instructionset(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr, 1) self.assertIsNot(gate, circuit.data[0].operation) self.assertEqual(circuit.data[0].operation.condition, (cr, 1)) @@ -287,7 +296,8 @@ def test_immutable_pickle(self): def test_mutable_pickle(self): gate = SXGate() clbit = Clbit() - condition_gate = gate.c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + condition_gate = gate.c_if(clbit, 0) self.assertIsNot(gate, condition_gate) self.assertEqual(condition_gate.condition, (clbit, 0)) self.assertTrue(condition_gate.mutable) @@ -505,7 +515,8 @@ def test_label_not_singleton(self): def test_condition_not_singleton(self): gate = CZGate() - condition_gate = CZGate().c_if(Clbit(), 0) + with self.assertWarns(DeprecationWarning): + condition_gate = CZGate().c_if(Clbit(), 0) self.assertIsNot(gate, condition_gate) def test_raise_on_state_mutation(self): @@ -518,7 +529,8 @@ def test_raise_on_state_mutation(self): def test_labeled_condition(self): singleton_gate = CSwapGate() clbit = Clbit() - gate = CSwapGate(label="conditionally special").c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + gate = CSwapGate(label="conditionally special").c_if(clbit, 0) self.assertIsNot(singleton_gate, gate) self.assertEqual(gate.label, "conditionally special") self.assertEqual(gate.condition, (clbit, 0)) @@ -551,14 +563,16 @@ def test_label_copy_new(self): self.assertEqual(copied_label.label, "special") def test_condition_copy(self): - gate = CZGate().c_if(Clbit(), 0) + with self.assertWarns(DeprecationWarning): + gate = CZGate().c_if(Clbit(), 0) copied = gate.copy() self.assertIsNot(gate, copied) self.assertEqual(gate, copied) def test_condition_label_copy(self): clbit = Clbit() - gate = CZGate(label="conditionally special").c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + gate = CZGate(label="conditionally special").c_if(clbit, 0) copied = gate.copy() self.assertIsNot(gate, copied) self.assertEqual(gate, copied) @@ -583,14 +597,16 @@ def test_deepcopy_with_label(self): self.assertNotEqual(singleton_gate.label, copied.label) def test_deepcopy_with_condition(self): - gate = CCXGate().c_if(Clbit(), 0) + with self.assertWarns(DeprecationWarning): + gate = CCXGate().c_if(Clbit(), 0) copied = copy.deepcopy(gate) self.assertIsNot(gate, copied) self.assertEqual(gate, copied) def test_condition_label_deepcopy(self): clbit = Clbit() - gate = CHGate(label="conditionally special").c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + gate = CHGate(label="conditionally special").c_if(clbit, 0) copied = copy.deepcopy(gate) self.assertIsNot(gate, copied) self.assertEqual(gate, copied) @@ -640,7 +656,8 @@ def test_round_trip_dag_conversion_with_label(self): def test_round_trip_dag_conversion_with_condition(self): qc = QuantumCircuit(2, 1) - gate = CHGate().c_if(qc.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + gate = CHGate().c_if(qc.cregs[0], 0) qc.append(gate, [0, 1]) dag = circuit_to_dag(qc) out = dag_to_circuit(dag) @@ -650,7 +667,8 @@ def test_round_trip_dag_conversion_with_condition(self): def test_round_trip_dag_conversion_condition_label(self): qc = QuantumCircuit(2, 1) - gate = CHGate(label="conditionally special").c_if(qc.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + gate = CHGate(label="conditionally special").c_if(qc.cregs[0], 0) qc.append(gate, [0, 1]) dag = circuit_to_dag(qc) out = dag_to_circuit(dag) @@ -664,7 +682,8 @@ def test_condition_via_instructionset(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr, 1) self.assertIsNot(gate, circuit.data[0].operation) self.assertEqual(circuit.data[0].operation.condition, (cr, 1)) @@ -723,7 +742,8 @@ def test_inner_outer_label_with_c_if(self): inner_gate = HGate(label="my h gate") controlled_gate = inner_gate.control(label="foo") clbit = Clbit() - conditonal_controlled_gate = controlled_gate.c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + conditonal_controlled_gate = controlled_gate.c_if(clbit, 0) self.assertTrue(conditonal_controlled_gate.mutable) self.assertEqual("my h gate", conditonal_controlled_gate.base_gate.label) self.assertEqual("foo", conditonal_controlled_gate.label) @@ -733,7 +753,8 @@ def test_inner_outer_label_with_c_if_deepcopy(self): inner_gate = XGate(label="my h gate") controlled_gate = inner_gate.control(label="foo") clbit = Clbit() - conditonal_controlled_gate = controlled_gate.c_if(clbit, 0) + with self.assertWarns(DeprecationWarning): + conditonal_controlled_gate = controlled_gate.c_if(clbit, 0) self.assertTrue(conditonal_controlled_gate.mutable) self.assertEqual("my h gate", conditonal_controlled_gate.base_gate.label) self.assertEqual("foo", conditonal_controlled_gate.label) diff --git a/test/python/circuit/test_store.py b/test/python/circuit/test_store.py index 139192745d2e..ecb98681bbd2 100644 --- a/test/python/circuit/test_store.py +++ b/test/python/circuit/test_store.py @@ -73,7 +73,8 @@ def test_rejects_dangerous_cast(self): def test_rejects_c_if(self): instruction = Store(expr.Var.new("a", types.Bool()), expr.Var.new("b", types.Bool())) with self.assertRaises(NotImplementedError): - instruction.c_if(Clbit(), False) + with self.assertWarns(DeprecationWarning): + instruction.c_if(Clbit(), False) class TestStoreCircuit(QiskitTestCase): @@ -241,4 +242,5 @@ def test_rejects_c_if(self): qc = QuantumCircuit([Clbit()], inputs=[a]) instruction_set = qc.store(a, True) with self.assertRaises(NotImplementedError): - instruction_set.c_if(qc.clbits[0], False) + with self.assertWarns(DeprecationWarning): + instruction_set.c_if(qc.clbits[0], False) diff --git a/test/python/compiler/test_assembler.py b/test/python/compiler/test_assembler.py index c333ce9ace22..1ae0e327d8e7 100644 --- a/test/python/compiler/test_assembler.py +++ b/test/python/compiler/test_assembler.py @@ -284,7 +284,8 @@ def test_measure_to_registers_when_conditionals(self): qc.measure(qr[0], cr1) # Measure not required for a later conditional qc.measure(qr[1], cr2[1]) # Measure required for a later conditional - qc.h(qr[1]).c_if(cr2, 3) + with self.assertWarns(DeprecationWarning): + qc.h(qr[1]).c_if(cr2, 3) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) @@ -304,7 +305,8 @@ def test_convert_to_bfunc_plus_conditional(self): cr = ClassicalRegister(1) qc = QuantumCircuit(qr, cr) - qc.h(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.h(qr[0]).c_if(cr, 1) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) @@ -325,7 +327,8 @@ def test_convert_to_bfunc_plus_conditional_onebit(self): cr = ClassicalRegister(3) qc = QuantumCircuit(qr, cr) - qc.h(qr[0]).c_if(cr[2], 1) + with self.assertWarns(DeprecationWarning): + qc.h(qr[0]).c_if(cr[2], 1) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) @@ -351,7 +354,8 @@ def test_resize_value_to_register(self): cr3 = ClassicalRegister(1) qc = QuantumCircuit(qr, cr1, cr2, cr3) - qc.h(qr[0]).c_if(cr2, 2) + with self.assertWarns(DeprecationWarning): + qc.h(qr[0]).c_if(cr2, 2) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) diff --git a/test/python/compiler/test_disassembler.py b/test/python/compiler/test_disassembler.py index 0525b54e2107..bfeeeacf595c 100644 --- a/test/python/compiler/test_disassembler.py +++ b/test/python/compiler/test_disassembler.py @@ -191,7 +191,8 @@ def test_circuit_with_conditionals(self): qc = QuantumCircuit(qr, cr1, cr2) qc.measure(qr[0], cr1) # Measure not required for a later conditional qc.measure(qr[1], cr2[1]) # Measure required for a later conditional - qc.h(qr[1]).c_if(cr2, 3) + with self.assertWarns(DeprecationWarning): + qc.h(qr[1]).c_if(cr2, 3) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) circuits, run_config_out, header = disassemble(qobj) @@ -207,7 +208,8 @@ def test_circuit_with_simple_conditional(self): qr = QuantumRegister(1) cr = ClassicalRegister(1) qc = QuantumCircuit(qr, cr) - qc.h(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.h(qr[0]).c_if(cr, 1) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) circuits, run_config_out, header = disassemble(qobj) @@ -228,7 +230,8 @@ def test_circuit_with_single_bit_conditions(self): qr = QuantumRegister(1) cr = ClassicalRegister(2) qc = QuantumCircuit(qr, cr) - qc.h(qr[0]).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + qc.h(qr[0]).c_if(cr[0], 1) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) @@ -267,9 +270,12 @@ def test_multiple_conditionals_multiple_registers(self): qc = QuantumCircuit(qr, cr1, cr2, cr3, cr4) qc.x(qr[1]) qc.h(qr) - qc.cx(qr[1], qr[0]).c_if(cr3, 14) - qc.ccx(qr[0], qr[2], qr[1]).c_if(cr4, 1) - qc.h(qr).c_if(cr1, 3) + with self.assertWarns(DeprecationWarning): + qc.cx(qr[1], qr[0]).c_if(cr3, 14) + with self.assertWarns(DeprecationWarning): + qc.ccx(qr[0], qr[2], qr[1]).c_if(cr4, 1) + with self.assertWarns(DeprecationWarning): + qc.h(qr).c_if(cr1, 3) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) circuits, run_config_out, header = disassemble(qobj) @@ -285,7 +291,8 @@ def test_circuit_with_bit_conditional_1(self): qr = QuantumRegister(2) cr = ClassicalRegister(2) qc = QuantumCircuit(qr, cr) - qc.h(qr[0]).c_if(cr[1], True) + with self.assertWarns(DeprecationWarning): + qc.h(qr[0]).c_if(cr[1], True) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) circuits, run_config_out, header = disassemble(qobj) @@ -302,9 +309,12 @@ def test_circuit_with_bit_conditional_2(self): cr = ClassicalRegister(2) cr1 = ClassicalRegister(2) qc = QuantumCircuit(qr, cr, cr1) - qc.h(qr[0]).c_if(cr1[1], False) - qc.h(qr[1]).c_if(cr[0], True) - qc.cx(qr[0], qr[1]).c_if(cr1[0], False) + with self.assertWarns(DeprecationWarning): + qc.h(qr[0]).c_if(cr1[1], False) + with self.assertWarns(DeprecationWarning): + qc.h(qr[1]).c_if(cr[0], True) + with self.assertWarns(DeprecationWarning): + qc.cx(qr[0], qr[1]).c_if(cr1[0], False) with self.assertWarns(DeprecationWarning): qobj = assemble(qc) circuits, run_config_out, header = disassemble(qobj) diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py index 825dfd6bdfe7..b7bf3365f1d9 100644 --- a/test/python/compiler/test_transpiler.py +++ b/test/python/compiler/test_transpiler.py @@ -2210,7 +2210,8 @@ def _regular_circuit(self): base.append(CustomCX(), [3, 6]) base.append(CustomCX(), [5, 4]) base.append(CustomCX(), [5, 3]) - base.append(CustomCX(), [2, 4]).c_if(base.cregs[0], 3) + with self.assertWarns(DeprecationWarning): + base.append(CustomCX(), [2, 4]).c_if(base.cregs[0], 3) base.ry(a, 4) base.measure(4, 2) return base @@ -2819,7 +2820,8 @@ def test_parallel_singleton_conditional_gate(self, opt_level): circ = QuantumCircuit(2, 1) circ.h(0) circ.measure(0, circ.clbits[0]) - circ.z(1).c_if(circ.clbits[0], 1) + with self.assertWarns(DeprecationWarning): + circ.z(1).c_if(circ.clbits[0], 1) res = transpile( [circ, circ], backend, optimization_level=opt_level, seed_transpiler=123456769 ) @@ -3267,7 +3269,8 @@ def test_shared_classical_between_components_condition(self, opt_level): for i in range(18): qc.measure(i, creg[i]) - qc.ecr(20, 21).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.ecr(20, 21).c_if(creg, 0) tqc = transpile(qc, self.backend, optimization_level=opt_level, seed_transpiler=42) def _visit_block(circuit, qubit_mapping=None): @@ -3303,9 +3306,11 @@ def test_shared_classical_between_components_condition_large_to_small(self, opt_ qc.measure(24, creg[0]) qc.measure(23, creg[1]) # Component 1 - qc.h(0).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.h(0).c_if(creg, 0) for i in range(18): - qc.ecr(0, i + 1).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.ecr(0, i + 1).c_if(creg, 0) tqc = transpile(qc, self.backend, optimization_level=opt_level, seed_transpiler=123456789) def _visit_block(circuit, qubit_mapping=None): @@ -3377,9 +3382,11 @@ def test_shared_classical_between_components_condition_large_to_small_reverse_in qc.measure(0, creg[0]) qc.measure(1, creg[1]) # Component 1 - qc.h(24).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.h(24).c_if(creg, 0) for i in range(23, 5, -1): - qc.ecr(24, i).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.ecr(24, i).c_if(creg, 0) tqc = transpile(qc, self.backend, optimization_level=opt_level, seed_transpiler=2023) def _visit_block(circuit, qubit_mapping=None): @@ -3450,15 +3457,19 @@ def test_chained_data_dependency(self, opt_level): measure_op = Measure() qc.append(measure_op, [9], [creg[0]]) # Component 1 - qc.h(10).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.h(10).c_if(creg, 0) for i in range(11, 20): - qc.ecr(10, i).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.ecr(10, i).c_if(creg, 0) measure_op = Measure() qc.append(measure_op, [19], [creg[0]]) # Component 2 - qc.h(20).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.h(20).c_if(creg, 0) for i in range(21, 30): - qc.cz(20, i).c_if(creg, 0) + with self.assertWarns(DeprecationWarning): + qc.cz(20, i).c_if(creg, 0) measure_op = Measure() qc.append(measure_op, [29], [creg[0]]) tqc = transpile(qc, self.backend, optimization_level=opt_level, seed_transpiler=2023) diff --git a/test/python/converters/test_circuit_to_dag.py b/test/python/converters/test_circuit_to_dag.py index 0bded9c0f4a2..c89d47727f0c 100644 --- a/test/python/converters/test_circuit_to_dag.py +++ b/test/python/converters/test_circuit_to_dag.py @@ -34,7 +34,8 @@ def test_circuit_and_dag(self): circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) - circuit_in.x(qr[0]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) diff --git a/test/python/converters/test_circuit_to_dagdependency.py b/test/python/converters/test_circuit_to_dagdependency.py index 9d33192d50a1..37ad23d5c47a 100644 --- a/test/python/converters/test_circuit_to_dagdependency.py +++ b/test/python/converters/test_circuit_to_dagdependency.py @@ -33,7 +33,8 @@ def test_circuit_and_dag_canonical(self): circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) - circuit_in.x(qr[0]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) @@ -51,7 +52,8 @@ def test_circuit_and_dag_canonical2(self): circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) - circuit_in.x(qr[0]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) diff --git a/test/python/converters/test_circuit_to_dagdependency_v2.py b/test/python/converters/test_circuit_to_dagdependency_v2.py index edeea3a59fea..9d72d045db31 100644 --- a/test/python/converters/test_circuit_to_dagdependency_v2.py +++ b/test/python/converters/test_circuit_to_dagdependency_v2.py @@ -33,7 +33,8 @@ def test_circuit_and_dag_canonical(self): circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) - circuit_in.x(qr[0]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) diff --git a/test/python/converters/test_circuit_to_instruction.py b/test/python/converters/test_circuit_to_instruction.py index e3239d4b5ff4..505e3d93bdb7 100644 --- a/test/python/converters/test_circuit_to_instruction.py +++ b/test/python/converters/test_circuit_to_instruction.py @@ -56,12 +56,16 @@ def test_flatten_registers_of_circuit_single_bit_cond(self): cr1 = ClassicalRegister(3, "cr1") cr2 = ClassicalRegister(3, "cr2") circ = QuantumCircuit(qr1, qr2, cr1, cr2) - circ.h(qr1[0]).c_if(cr1[1], True) - circ.h(qr2[1]).c_if(cr2[0], False) - circ.cx(qr1[1], qr2[2]).c_if(cr2[2], True) + with self.assertWarns(DeprecationWarning): + circ.h(qr1[0]).c_if(cr1[1], True) + with self.assertWarns(DeprecationWarning): + circ.h(qr2[1]).c_if(cr2[0], False) + with self.assertWarns(DeprecationWarning): + circ.cx(qr1[1], qr2[2]).c_if(cr2[2], True) circ.measure(qr2[2], cr2[0]) - inst = circuit_to_instruction(circ) + with self.assertWarns(DeprecationWarning): + inst = circuit_to_instruction(circ) q = QuantumRegister(5, "q") c = ClassicalRegister(6, "c") @@ -196,8 +200,10 @@ def test_registerless_classical_bits(self): Regression test of gh-7394.""" expected = QuantumCircuit([Qubit(), Clbit()]) - expected.h(0).c_if(expected.clbits[0], 0) - test = circuit_to_instruction(expected) + with self.assertWarns(DeprecationWarning): + expected.h(0).c_if(expected.clbits[0], 0) + with self.assertWarns(DeprecationWarning): + test = circuit_to_instruction(expected) self.assertIsInstance(test, Instruction) self.assertIsInstance(test.definition, QuantumCircuit) diff --git a/test/python/converters/test_dag_to_dagdependency.py b/test/python/converters/test_dag_to_dagdependency.py index 6b52652e6ace..b4a66659245b 100644 --- a/test/python/converters/test_dag_to_dagdependency.py +++ b/test/python/converters/test_dag_to_dagdependency.py @@ -34,7 +34,8 @@ def test_circuit_and_dag_dependency(self): circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) - circuit_in.x(qr[0]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) @@ -55,7 +56,8 @@ def test_circuit_and_dag_dependency2(self): circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) - circuit_in.x(qr[0]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) diff --git a/test/python/converters/test_dag_to_dagdependency_v2.py b/test/python/converters/test_dag_to_dagdependency_v2.py index 925bf442f477..951e31835fe9 100644 --- a/test/python/converters/test_dag_to_dagdependency_v2.py +++ b/test/python/converters/test_dag_to_dagdependency_v2.py @@ -34,7 +34,8 @@ def test_circuit_and_dag_dependency(self): circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) - circuit_in.x(qr[0]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) diff --git a/test/python/dagcircuit/test_collect_blocks.py b/test/python/dagcircuit/test_collect_blocks.py index b2715078d7f5..4650a7203c22 100644 --- a/test/python/dagcircuit/test_collect_blocks.py +++ b/test/python/dagcircuit/test_collect_blocks.py @@ -243,7 +243,8 @@ def test_circuit_has_conditional_gates(self): qc.x(0) qc.x(1) qc.cx(1, 0) - qc.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 1) qc.x(0) qc.x(1) qc.cx(0, 1) @@ -280,7 +281,8 @@ def test_circuit_has_conditional_gates_dagdependency(self): qc.x(0) qc.x(1) qc.cx(1, 0) - qc.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 1) qc.x(0) qc.x(1) qc.cx(0, 1) @@ -544,11 +546,13 @@ def test_collect_blocks_with_clbits(self): condition.""" qc = QuantumCircuit(4, 3) - qc.cx(0, 1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(0, 1) qc.cx(2, 3) qc.cx(1, 2) qc.cx(0, 1) - qc.cx(2, 3).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(2, 3).c_if(1, 0) dag = circuit_to_dag(qc) @@ -567,7 +571,8 @@ def _collapse_fn(circuit): return op # Collapse block with measures into a single "COLLAPSED" block - dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) + with self.assertWarns(DeprecationWarning): + dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) collapsed_qc = dag_to_circuit(dag) self.assertEqual(len(collapsed_qc.data), 1) @@ -580,11 +585,13 @@ def test_collect_blocks_with_clbits_dagdependency(self): under conditions, using DAGDependency.""" qc = QuantumCircuit(4, 3) - qc.cx(0, 1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(0, 1) qc.cx(2, 3) qc.cx(1, 2) qc.cx(0, 1) - qc.cx(2, 3).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(2, 3).c_if(1, 0) dag = circuit_to_dagdependency(qc) @@ -603,7 +610,8 @@ def _collapse_fn(circuit): return op # Collapse block with measures into a single "COLLAPSED" block - dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) + with self.assertWarns(DeprecationWarning): + dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) collapsed_qc = dagdependency_to_circuit(dag) self.assertEqual(len(collapsed_qc.data), 1) @@ -620,10 +628,13 @@ def test_collect_blocks_with_clbits2(self): cbit = Clbit() qc = QuantumCircuit(qreg, creg, [cbit]) - qc.cx(0, 1).c_if(creg[1], 1) - qc.cx(2, 3).c_if(cbit, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg[1], 1) + with self.assertWarns(DeprecationWarning): + qc.cx(2, 3).c_if(cbit, 0) qc.cx(1, 2) - qc.cx(0, 1).c_if(creg[2], 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg[2], 1) dag = circuit_to_dag(qc) @@ -642,7 +653,8 @@ def _collapse_fn(circuit): return op # Collapse block with measures into a single "COLLAPSED" block - dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) + with self.assertWarns(DeprecationWarning): + dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) collapsed_qc = dag_to_circuit(dag) self.assertEqual(len(collapsed_qc.data), 1) @@ -659,10 +671,13 @@ def test_collect_blocks_with_clbits2_dagdependency(self): cbit = Clbit() qc = QuantumCircuit(qreg, creg, [cbit]) - qc.cx(0, 1).c_if(creg[1], 1) - qc.cx(2, 3).c_if(cbit, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg[1], 1) + with self.assertWarns(DeprecationWarning): + qc.cx(2, 3).c_if(cbit, 0) qc.cx(1, 2) - qc.cx(0, 1).c_if(creg[2], 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg[2], 1) dag = circuit_to_dag(qc) @@ -681,7 +696,8 @@ def _collapse_fn(circuit): return op # Collapse block with measures into a single "COLLAPSED" block - dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) + with self.assertWarns(DeprecationWarning): + dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) collapsed_qc = dag_to_circuit(dag) self.assertEqual(len(collapsed_qc.data), 1) @@ -698,9 +714,11 @@ def test_collect_blocks_with_cregs(self): creg2 = ClassicalRegister(2, "cr2") qc = QuantumCircuit(qreg, creg, creg2) - qc.cx(0, 1).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg, 3) qc.cx(1, 2) - qc.cx(0, 1).c_if(creg[2], 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg[2], 1) dag = circuit_to_dag(qc) @@ -719,7 +737,8 @@ def _collapse_fn(circuit): return op # Collapse block with measures into a single "COLLAPSED" block - dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) + with self.assertWarns(DeprecationWarning): + dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) collapsed_qc = dag_to_circuit(dag) self.assertEqual(len(collapsed_qc.data), 1) @@ -737,9 +756,11 @@ def test_collect_blocks_with_cregs_dagdependency(self): creg2 = ClassicalRegister(2, "cr2") qc = QuantumCircuit(qreg, creg, creg2) - qc.cx(0, 1).c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg, 3) qc.cx(1, 2) - qc.cx(0, 1).c_if(creg[2], 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(creg[2], 1) dag = circuit_to_dagdependency(qc) @@ -758,7 +779,8 @@ def _collapse_fn(circuit): return op # Collapse block with measures into a single "COLLAPSED" block - dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) + with self.assertWarns(DeprecationWarning): + dag = BlockCollapser(dag).collapse_to_operation(blocks, _collapse_fn) collapsed_qc = dagdependency_to_circuit(dag) self.assertEqual(len(collapsed_qc.data), 1) @@ -917,14 +939,19 @@ def test_split_layers_dagdependency(self): def test_block_collapser_register_condition(self): """Test that BlockCollapser can handle a register being used more than once.""" qc = QuantumCircuit(1, 2) - qc.x(0).c_if(qc.cregs[0], 0) - qc.y(0).c_if(qc.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(qc.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + qc.y(0).c_if(qc.cregs[0], 1) dag = circuit_to_dag(qc) blocks = BlockCollector(dag).collect_all_matching_blocks( lambda _: True, split_blocks=False, min_block_size=1 ) - dag = BlockCollapser(dag).collapse_to_operation(blocks, lambda circ: circ.to_instruction()) + with self.assertWarns(DeprecationWarning): + dag = BlockCollapser(dag).collapse_to_operation( + blocks, lambda circ: circ.to_instruction() + ) collapsed_qc = dag_to_circuit(dag) self.assertEqual(len(collapsed_qc.data), 1) diff --git a/test/python/dagcircuit/test_compose.py b/test/python/dagcircuit/test_compose.py index ff5014eacef7..82246b306f47 100644 --- a/test/python/dagcircuit/test_compose.py +++ b/test/python/dagcircuit/test_compose.py @@ -321,8 +321,10 @@ def test_compose_conditional(self): creg = ClassicalRegister(2, "rcr") circuit_right = QuantumCircuit(qreg, creg) - circuit_right.x(qreg[1]).c_if(creg, 2) - circuit_right.h(qreg[0]).c_if(creg, 1) + with self.assertWarns(DeprecationWarning): + circuit_right.x(qreg[1]).c_if(creg, 2) + with self.assertWarns(DeprecationWarning): + circuit_right.h(qreg[0]).c_if(creg, 1) circuit_right.measure(qreg, creg) # permuted subset of qubits and clbits @@ -330,16 +332,19 @@ def test_compose_conditional(self): dag_right = circuit_to_dag(circuit_right) # permuted subset of qubits and clbits - dag_left.compose( - dag_right, - qubits=[self.left_qubit1, self.left_qubit4], - clbits=[self.left_clbit1, self.left_clbit0], - ) + with self.assertWarns(DeprecationWarning): + dag_left.compose( + dag_right, + qubits=[self.left_qubit1, self.left_qubit4], + clbits=[self.left_clbit1, self.left_clbit0], + ) circuit_composed = dag_to_circuit(dag_left) circuit_expected = self.circuit_left.copy() - circuit_expected.x(self.left_qubit4).c_if(*self.condition1) - circuit_expected.h(self.left_qubit1).c_if(*self.condition2) + with self.assertWarns(DeprecationWarning): + circuit_expected.x(self.left_qubit4).c_if(*self.condition1) + with self.assertWarns(DeprecationWarning): + circuit_expected.h(self.left_qubit1).c_if(*self.condition2) circuit_expected.measure(self.left_qubit4, self.left_clbit0) circuit_expected.measure(self.left_qubit1, self.left_clbit1) @@ -423,12 +428,13 @@ def test_compose_condition_multiple_classical(self): circuit_left = QuantumCircuit(qreg, creg1, creg2) circuit_right = QuantumCircuit(qreg, creg1, creg2) - circuit_right.h(0).c_if(creg1, 1) + with self.assertWarns(DeprecationWarning): + circuit_right.h(0).c_if(creg1, 1) dag_left = circuit_to_dag(circuit_left) dag_right = circuit_to_dag(circuit_right) - - dag_composed = dag_left.compose(dag_right, qubits=[0], clbits=[0, 1], inplace=False) + with self.assertWarns(DeprecationWarning): + dag_composed = dag_left.compose(dag_right, qubits=[0], clbits=[0, 1], inplace=False) dag_expected = circuit_to_dag(circuit_right.copy()) diff --git a/test/python/dagcircuit/test_dagcircuit.py b/test/python/dagcircuit/test_dagcircuit.py index ef8050961066..10a0ebf06fd6 100644 --- a/test/python/dagcircuit/test_dagcircuit.py +++ b/test/python/dagcircuit/test_dagcircuit.py @@ -561,7 +561,8 @@ def setUp(self): def test_apply_operation_back(self): """The apply_operation_back() method.""" - x_gate = XGate().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + x_gate = XGate().c_if(*self.condition) self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CXGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Measure(), [self.qubit1], [self.clbit1]) @@ -573,7 +574,8 @@ def test_apply_operation_back(self): def test_edges(self): """Test that DAGCircuit.edges() behaves as expected with ops.""" - x_gate = XGate().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + x_gate = XGate().c_if(*self.condition) self.dag.apply_operation_back(HGate(), [self.qubit0], []) self.dag.apply_operation_back(CXGate(), [self.qubit0, self.qubit1], []) self.dag.apply_operation_back(Measure(), [self.qubit1], [self.clbit1]) @@ -590,8 +592,8 @@ def test_apply_operation_back_conditional(self): """Test consistency of apply_operation_back with condition set.""" # Single qubit gate conditional: qc.h(qr[2]).c_if(cr, 3) - - h_gate = HGate().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + h_gate = HGate().c_if(*self.condition) h_node = self.dag.apply_operation_back(h_gate, [self.qubit2], []) self.assertEqual(h_node.qargs, (self.qubit2,)) @@ -630,8 +632,8 @@ def test_apply_operation_back_conditional_measure(self): new_creg = ClassicalRegister(1, "cr2") self.dag.add_creg(new_creg) - - meas_gate = Measure().c_if(new_creg, 0) + with self.assertWarns(DeprecationWarning): + meas_gate = Measure().c_if(new_creg, 0) meas_node = self.dag.apply_operation_back(meas_gate, [self.qubit0], [self.clbit0]) self.assertEqual(meas_node.qargs, (self.qubit0,)) @@ -675,8 +677,8 @@ def test_apply_operation_back_conditional_measure_to_self(self): # Measure targeting a clbit which _is_ a member of the conditional # register. qc.measure(qr[0], cr[0]).c_if(cr, 3) - - meas_gate = Measure().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + meas_gate = Measure().c_if(*self.condition) meas_node = self.dag.apply_operation_back(meas_gate, [self.qubit1], [self.clbit1]) self.assertEqual(meas_node.qargs, (self.qubit1,)) @@ -1239,7 +1241,8 @@ def test_dag_collect_runs(self): def test_dag_collect_runs_start_with_conditional(self): """Test collect runs with a conditional at the start of the run.""" - h_gate = HGate().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + h_gate = HGate().c_if(*self.condition) self.dag.apply_operation_back(h_gate, [self.qubit0]) self.dag.apply_operation_back(HGate(), [self.qubit0]) self.dag.apply_operation_back(HGate(), [self.qubit0]) @@ -1252,7 +1255,8 @@ def test_dag_collect_runs_start_with_conditional(self): def test_dag_collect_runs_conditional_in_middle(self): """Test collect_runs with a conditional in the middle of a run.""" - h_gate = HGate().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + h_gate = HGate().c_if(*self.condition) self.dag.apply_operation_back(HGate(), [self.qubit0]) self.dag.apply_operation_back(h_gate, [self.qubit0]) self.dag.apply_operation_back(HGate(), [self.qubit0]) @@ -1294,7 +1298,8 @@ def test_dag_collect_1q_runs_start_with_conditional(self): """Test collect 1q runs with a conditional at the start of the run.""" self.dag.apply_operation_back(Reset(), [self.qubit0]) self.dag.apply_operation_back(Delay(100), [self.qubit0]) - h_gate = HGate().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + h_gate = HGate().c_if(*self.condition) self.dag.apply_operation_back(h_gate, [self.qubit0]) self.dag.apply_operation_back(HGate(), [self.qubit0]) self.dag.apply_operation_back(HGate(), [self.qubit0]) @@ -1309,7 +1314,8 @@ def test_dag_collect_1q_runs_conditional_in_middle(self): """Test collect_1q_runs with a conditional in the middle of a run.""" self.dag.apply_operation_back(Reset(), [self.qubit0]) self.dag.apply_operation_back(Delay(100), [self.qubit0]) - h_gate = HGate().c_if(*self.condition) + with self.assertWarns(DeprecationWarning): + h_gate = HGate().c_if(*self.condition) self.dag.apply_operation_back(HGate(), [self.qubit0]) self.dag.apply_operation_back(h_gate, [self.qubit0]) self.dag.apply_operation_back(HGate(), [self.qubit0]) @@ -1387,7 +1393,8 @@ def test_layers_basic(self): qubit1 = qreg[1] clbit0 = creg[0] clbit1 = creg[1] - x_gate = XGate().c_if(creg, 3) + with self.assertWarns(DeprecationWarning): + x_gate = XGate().c_if(creg, 3) dag = DAGCircuit() dag.add_qreg(qreg) dag.add_creg(creg) @@ -1856,29 +1863,41 @@ def test_semantic_conditions(self): qreg = QuantumRegister(1, name="q") creg = ClassicalRegister(1, name="c") qc1 = QuantumCircuit(qreg, creg, [Clbit()]) - qc1.x(0).c_if(qc1.cregs[0], 1) - qc1.x(0).c_if(qc1.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.clbits[-1], True) qc2 = QuantumCircuit(qreg, creg, [Clbit()]) - qc2.x(0).c_if(qc2.cregs[0], 1) - qc2.x(0).c_if(qc2.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.clbits[-1], True) self.assertEqual(circuit_to_dag(qc1), circuit_to_dag(qc2)) # Order of operations transposed. qc1 = QuantumCircuit(qreg, creg, [Clbit()]) - qc1.x(0).c_if(qc1.cregs[0], 1) - qc1.x(0).c_if(qc1.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.clbits[-1], True) qc2 = QuantumCircuit(qreg, creg, [Clbit()]) - qc2.x(0).c_if(qc2.clbits[-1], True) - qc2.x(0).c_if(qc2.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.cregs[0], 1) self.assertNotEqual(circuit_to_dag(qc1), circuit_to_dag(qc2)) # Single-bit condition values not the same. qc1 = QuantumCircuit(qreg, creg, [Clbit()]) - qc1.x(0).c_if(qc1.cregs[0], 1) - qc1.x(0).c_if(qc1.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc1.x(0).c_if(qc1.clbits[-1], True) qc2 = QuantumCircuit(qreg, creg, [Clbit()]) - qc2.x(0).c_if(qc2.cregs[0], 1) - qc2.x(0).c_if(qc2.clbits[-1], False) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc2.x(0).c_if(qc2.clbits[-1], False) self.assertNotEqual(circuit_to_dag(qc1), circuit_to_dag(qc2)) def test_semantic_expr(self): @@ -2489,7 +2508,8 @@ def test_substitute_without_propagating_bit_conditional(self): sub = QuantumCircuit(2, 1) sub.h(0) - sub.cx(0, 1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + sub.cx(0, 1).c_if(0, True) sub.h(0) expected = DAGCircuit() @@ -2522,7 +2542,8 @@ def test_substitute_without_propagating_register_conditional(self): sub = QuantumCircuit(QuantumRegister(2), ClassicalRegister(2)) sub.h(0) - sub.cx(0, 1).c_if(sub.cregs[0], 3) + with self.assertWarns(DeprecationWarning): + sub.cx(0, 1).c_if(sub.cregs[0], 3) sub.h(0) expected = DAGCircuit() @@ -2559,8 +2580,10 @@ def test_substitute_with_provided_wire_map_propagate_condition(self): sub.cx(0, 1) sub.h(0) - conditioned_h = HGate().c_if(*conditioned_cz.condition) - conditioned_cx = CXGate().c_if(*conditioned_cz.condition) + with self.assertWarns(DeprecationWarning): + conditioned_h = HGate().c_if(*conditioned_cz.condition) + with self.assertWarns(DeprecationWarning): + conditioned_cx = CXGate().c_if(*conditioned_cz.condition) expected = DAGCircuit() expected.add_qubits(base_qubits) @@ -2593,7 +2616,8 @@ def test_substitute_with_provided_wire_map_no_propagate_condition(self): sub = QuantumCircuit(2, 1) sub.h(0) - sub.cx(0, 1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + sub.cx(0, 1).c_if(0, True) sub.h(0) conditioned_cx = CXGate().to_mutable() @@ -2626,7 +2650,8 @@ def test_creates_additional_alias_register(self): target = base.apply_operation_back(Instruction("dummy", 2, 2, []), base_qreg, base_creg[:2]) sub = QuantumCircuit(QuantumRegister(2), ClassicalRegister(2)) - sub.cx(0, 1).c_if(sub.cregs[0], 3) + with self.assertWarns(DeprecationWarning): + sub.cx(0, 1).c_if(sub.cregs[0], 3) base.substitute_node_with_dag(target, circuit_to_dag(sub)) @@ -2731,12 +2756,14 @@ def test_refuses_to_overwrite_condition(self, inplace): dag = DAGCircuit() dag.add_qreg(qr) dag.add_creg(cr) - node = dag.apply_operation_back(XGate().c_if(cr, 2), qr, []) + with self.assertWarns(DeprecationWarning): + node = dag.apply_operation_back(XGate().c_if(cr, 2), qr, []) with self.assertRaisesRegex(DAGCircuitError, "Cannot propagate a condition"): - dag.substitute_node( - node, XGate().c_if(cr, 1), inplace=inplace, propagate_condition=True - ) + with self.assertWarns(DeprecationWarning): + dag.substitute_node( + node, XGate().c_if(cr, 1), inplace=inplace, propagate_condition=True + ) @data(True, False) def test_replace_if_else_op_with_another(self, inplace): @@ -3194,7 +3221,8 @@ def setUp(self): def test_creg_conditional(self): """Test consistency of conditional on classical register.""" - self.circuit.h(self.qreg[0]).c_if(self.creg, 1) + with self.assertWarns(DeprecationWarning): + self.circuit.h(self.qreg[0]).c_if(self.creg, 1) self.dag = circuit_to_dag(self.circuit) gate_node = self.dag.gate_nodes()[0] self.assertEqual(gate_node.op, HGate()) @@ -3235,8 +3263,8 @@ def test_creg_conditional(self): def test_clbit_conditional(self): """Test consistency of conditional on single classical bit.""" - - self.circuit.h(self.qreg[0]).c_if(self.creg[0], 1) + with self.assertWarns(DeprecationWarning): + self.circuit.h(self.qreg[0]).c_if(self.creg[0], 1) self.dag = circuit_to_dag(self.circuit) gate_node = self.dag.gate_nodes()[0] self.assertEqual(gate_node.op, HGate()) diff --git a/test/python/primitives/test_backend_sampler_v2.py b/test/python/primitives/test_backend_sampler_v2.py index 372ae3a6715c..22c4b8d5d684 100644 --- a/test/python/primitives/test_backend_sampler_v2.py +++ b/test/python/primitives/test_backend_sampler_v2.py @@ -1215,8 +1215,10 @@ def test_circuit_with_aliased_cregs(self, backend): qc.h(0) qc.measure(0, c1) qc.measure(1, c2) - qc.z(2).c_if(c1, 1) - qc.x(2).c_if(c2, 1) + with self.assertWarns(DeprecationWarning): + qc.z(2).c_if(c1, 1) + with self.assertWarns(DeprecationWarning): + qc.x(2).c_if(c2, 1) qc2 = QuantumCircuit(5, 5) qc2.compose(qc, [0, 2, 3], [2, 4], inplace=True) cregs = [creg.name for creg in qc2.cregs] @@ -1251,8 +1253,10 @@ def test_circuit_with_aliased_cregs_v1(self, backend): qc.h(0) qc.measure(0, c1) qc.measure(1, c2) - qc.z(2).c_if(c1, 1) - qc.x(2).c_if(c2, 1) + with self.assertWarns(DeprecationWarning): + qc.z(2).c_if(c1, 1) + with self.assertWarns(DeprecationWarning): + qc.x(2).c_if(c2, 1) qc2 = QuantumCircuit(5, 5) qc2.compose(qc, [0, 2, 3], [2, 4], inplace=True) cregs = [creg.name for creg in qc2.cregs] diff --git a/test/python/primitives/test_primitive.py b/test/python/primitives/test_primitive.py index fb96081fa001..dfca63a5d4b8 100644 --- a/test/python/primitives/test_primitive.py +++ b/test/python/primitives/test_primitive.py @@ -159,7 +159,8 @@ def test_circuit_key_controlflow(self): qc.h(0) qc.cx(0, 1) qc.measure(0, 0) - qc.break_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.break_loop().c_if(0, True) self.assertIsInstance(hash(_circuit_key(qc)), int) self.assertIsInstance(json.dumps(_circuit_key(qc)), str) diff --git a/test/python/primitives/test_statevector_sampler.py b/test/python/primitives/test_statevector_sampler.py index a782aafaeaf5..3af4e8d9f688 100644 --- a/test/python/primitives/test_statevector_sampler.py +++ b/test/python/primitives/test_statevector_sampler.py @@ -283,7 +283,8 @@ def test_run_errors(self): qc4 = QuantumCircuit(2, 2) qc4.h(0) qc4.measure(1, 1) - qc4.x(0).c_if(1, 1) + with self.assertWarns(DeprecationWarning): + qc4.x(0).c_if(1, 1) qc4.measure(0, 0) sampler = StatevectorSampler() @@ -592,8 +593,10 @@ def test_circuit_with_aliased_cregs(self): c2 = ClassicalRegister(1, "c2") qc = QuantumCircuit(q, c1, c2) - qc.z(2).c_if(c1, 1) - qc.x(2).c_if(c2, 1) + with self.assertWarns(DeprecationWarning): + qc.z(2).c_if(c1, 1) + with self.assertWarns(DeprecationWarning): + qc.x(2).c_if(c2, 1) qc2 = QuantumCircuit(5, 5) qc2.compose(qc, [0, 2, 3], [2, 4], inplace=True) # Note: qc2 has aliased cregs, c0 -> c[2] and c1 -> c[4]. diff --git a/test/python/providers/basic_provider/test_basic_simulator.py b/test/python/providers/basic_provider/test_basic_simulator.py index 57dd67dfd3c3..925823b49300 100644 --- a/test/python/providers/basic_provider/test_basic_simulator.py +++ b/test/python/providers/basic_provider/test_basic_simulator.py @@ -174,7 +174,8 @@ def test_if_statement(self): circuit_if_true.x(qr[1]) circuit_if_true.measure(qr[0], cr[0]) circuit_if_true.measure(qr[1], cr[1]) - circuit_if_true.x(qr[2]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_if_true.x(qr[2]).c_if(cr, 0x3) circuit_if_true.measure(qr[0], cr[0]) circuit_if_true.measure(qr[1], cr[1]) circuit_if_true.measure(qr[2], cr[2]) @@ -193,7 +194,8 @@ def test_if_statement(self): circuit_if_false.x(qr[0]) circuit_if_false.measure(qr[0], cr[0]) circuit_if_false.measure(qr[1], cr[1]) - circuit_if_false.x(qr[2]).c_if(cr, 0x3) + with self.assertWarns(DeprecationWarning): + circuit_if_false.x(qr[2]).c_if(cr, 0x3) circuit_if_false.measure(qr[0], cr[0]) circuit_if_false.measure(qr[1], cr[1]) circuit_if_false.measure(qr[2], cr[2]) @@ -230,7 +232,8 @@ def test_bit_cif_crossaffect(self): circuit.x([qr[1], qr[2]]) circuit.measure(qr[1], cr[1]) circuit.measure(qr[2], cr[2]) - circuit.h(qr[0]).c_if(cr[0], True) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr[0], True) circuit.measure(qr[0], cr1[0]) job = self.backend.run(circuit, shots=shots, seed_simulator=self.seed) result = job.result().get_counts() @@ -269,8 +272,10 @@ def test_teleport(self): circuit.barrier(qr) circuit.measure(qr[0], cr0[0]) circuit.measure(qr[1], cr1[0]) - circuit.z(qr[2]).c_if(cr0, 1) - circuit.x(qr[2]).c_if(cr1, 1) + with self.assertWarns(DeprecationWarning): + circuit.z(qr[2]).c_if(cr0, 1) + with self.assertWarns(DeprecationWarning): + circuit.x(qr[2]).c_if(cr1, 1) circuit.measure(qr[2], cr2[0]) job = self.backend.run( transpile(circuit, self.backend), shots=shots, seed_simulator=self.seed diff --git a/test/python/qasm2/test_arxiv_examples.py b/test/python/qasm2/test_arxiv_examples.py index 197b842697e1..64df2ac2598b 100644 --- a/test/python/qasm2/test_arxiv_examples.py +++ b/test/python/qasm2/test_arxiv_examples.py @@ -72,7 +72,8 @@ def test_teleportation(self, parser): if(c1==1) x q[2]; post q[2]; measure q[2] -> c2[0];""" - parsed = parser(example) + with self.assertWarns(DeprecationWarning): + parsed = parser(example) post = gate_builder("post", [], QuantumCircuit([Qubit()])) @@ -90,8 +91,10 @@ def test_teleportation(self, parser): qc.h(q[0]) qc.measure(q[0], c0[0]) qc.measure(q[1], c1[0]) - qc.z(q[2]).c_if(c0, 1) - qc.x(q[2]).c_if(c1, 1) + with self.assertWarns(DeprecationWarning): + qc.z(q[2]).c_if(c0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(q[2]).c_if(c1, 1) qc.append(post(), [q[2]], []) qc.measure(q[2], c2[0]) @@ -119,7 +122,8 @@ def test_qft(self, parser): cu1(pi/2) q[3],q[2]; h q[3]; measure q -> c;""" - parsed = parser(example) + with self.assertWarns(DeprecationWarning): + parsed = parser(example) qc = QuantumCircuit(QuantumRegister(4, "q"), ClassicalRegister(4, "c")) qc.x(0) @@ -168,7 +172,8 @@ def test_inverse_qft_1(self, parser): if(c==7) u1(pi/2+pi/4+pi/8) q[3]; h q[3]; measure q[3] -> c[3];""" - parsed = parser(example) + with self.assertWarns(DeprecationWarning): + parsed = parser(example) q = QuantumRegister(4, "q") c = ClassicalRegister(4, "c") @@ -177,21 +182,32 @@ def test_inverse_qft_1(self, parser): qc.barrier(q) qc.h(q[0]) qc.measure(q[0], c[0]) - qc.append(U1Gate(math.pi / 2).c_if(c, 1), [q[1]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 2).c_if(c, 1), [q[1]]) qc.h(q[1]) qc.measure(q[1], c[1]) - qc.append(U1Gate(math.pi / 4).c_if(c, 1), [q[2]]) - qc.append(U1Gate(math.pi / 2).c_if(c, 2), [q[2]]) - qc.append(U1Gate(math.pi / 4 + math.pi / 2).c_if(c, 3), [q[2]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 4).c_if(c, 1), [q[2]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 2).c_if(c, 2), [q[2]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 4 + math.pi / 2).c_if(c, 3), [q[2]]) qc.h(q[2]) qc.measure(q[2], c[2]) - qc.append(U1Gate(math.pi / 8).c_if(c, 1), [q[3]]) - qc.append(U1Gate(math.pi / 4).c_if(c, 2), [q[3]]) - qc.append(U1Gate(math.pi / 8 + math.pi / 4).c_if(c, 3), [q[3]]) - qc.append(U1Gate(math.pi / 2).c_if(c, 4), [q[3]]) - qc.append(U1Gate(math.pi / 8 + math.pi / 2).c_if(c, 5), [q[3]]) - qc.append(U1Gate(math.pi / 4 + math.pi / 2).c_if(c, 6), [q[3]]) - qc.append(U1Gate(math.pi / 8 + math.pi / 4 + math.pi / 2).c_if(c, 7), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 8).c_if(c, 1), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 4).c_if(c, 2), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 8 + math.pi / 4).c_if(c, 3), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 2).c_if(c, 4), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 8 + math.pi / 2).c_if(c, 5), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 4 + math.pi / 2).c_if(c, 6), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 8 + math.pi / 4 + math.pi / 2).c_if(c, 7), [q[3]]) qc.h(q[3]) qc.measure(q[3], c[3]) @@ -224,7 +240,8 @@ def test_inverse_qft_2(self, parser): if(c2==1) u1(pi/2) q[3]; h q[3]; measure q[3] -> c3[0];""" - parsed = parser(example) + with self.assertWarns(DeprecationWarning): + parsed = parser(example) q = QuantumRegister(4, "q") c0 = ClassicalRegister(1, "c0") @@ -236,16 +253,22 @@ def test_inverse_qft_2(self, parser): qc.barrier(q) qc.h(q[0]) qc.measure(q[0], c0[0]) - qc.append(U1Gate(math.pi / 2).c_if(c0, 1), [q[1]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 2).c_if(c0, 1), [q[1]]) qc.h(q[1]) qc.measure(q[1], c1[0]) - qc.append(U1Gate(math.pi / 4).c_if(c0, 1), [q[2]]) - qc.append(U1Gate(math.pi / 2).c_if(c1, 1), [q[2]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 4).c_if(c0, 1), [q[2]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 2).c_if(c1, 1), [q[2]]) qc.h(q[2]) qc.measure(q[2], c2[0]) - qc.append(U1Gate(math.pi / 8).c_if(c0, 1), [q[3]]) - qc.append(U1Gate(math.pi / 4).c_if(c1, 1), [q[3]]) - qc.append(U1Gate(math.pi / 2).c_if(c2, 1), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 8).c_if(c0, 1), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 4).c_if(c1, 1), [q[3]]) + with self.assertWarns(DeprecationWarning): + qc.append(U1Gate(math.pi / 2).c_if(c2, 1), [q[3]]) qc.h(q[3]) qc.measure(q[3], c3[0]) @@ -292,7 +315,8 @@ def test_ripple_carry_adder(self, parser): measure b[2] -> ans[2]; measure b[3] -> ans[3]; measure cout[0] -> ans[4];""" - parsed = parser(example) + with self.assertWarns(DeprecationWarning): + parsed = parser(example) majority_definition = QuantumCircuit([Qubit(), Qubit(), Qubit()]) majority_definition.cx(2, 1) @@ -423,7 +447,8 @@ def test_error_correction(self, parser): if(syn==2) x q[2]; if(syn==3) x q[1]; measure q -> c;""" - parsed = parser(example) + with self.assertWarns(DeprecationWarning): + parsed = parser(example) syndrome_definition = QuantumCircuit([Qubit() for _ in [None] * 5]) syndrome_definition.cx(0, 3) @@ -442,9 +467,12 @@ def test_error_correction(self, parser): qc.barrier(q) qc.append(syndrome(), [q[0], q[1], q[2], a[0], a[1]]) qc.measure(a, syn) - qc.x(q[0]).c_if(syn, 1) - qc.x(q[2]).c_if(syn, 2) - qc.x(q[1]).c_if(syn, 3) + with self.assertWarns(DeprecationWarning): + qc.x(q[0]).c_if(syn, 1) + with self.assertWarns(DeprecationWarning): + qc.x(q[2]).c_if(syn, 2) + with self.assertWarns(DeprecationWarning): + qc.x(q[1]).c_if(syn, 3) qc.measure(q, c) self.assertEqual(parsed, qc) diff --git a/test/python/qasm2/test_circuit_methods.py b/test/python/qasm2/test_circuit_methods.py index 1ba9689ab3ef..d5fa814f7c52 100644 --- a/test/python/qasm2/test_circuit_methods.py +++ b/test/python/qasm2/test_circuit_methods.py @@ -180,14 +180,16 @@ def test_qasm_text_conditional(self): ) + "\n" ) - q_circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + q_circuit = QuantumCircuit.from_qasm_str(qasm_string) qr = QuantumRegister(1, "q") cr0 = ClassicalRegister(4, "c0") cr1 = ClassicalRegister(4, "c1") ref = QuantumCircuit(qr, cr0, cr1) ref.x(qr[0]) - ref.x(qr[0]).c_if(cr1, 4) + with self.assertWarns(DeprecationWarning): + ref.x(qr[0]).c_if(cr1, 4) self.assertEqual(len(q_circuit.cregs), 2) self.assertEqual(len(q_circuit.qregs), 1) diff --git a/test/python/qasm2/test_export.py b/test/python/qasm2/test_export.py index 8de4bb8eb34f..ef6ab8076ef6 100644 --- a/test/python/qasm2/test_export.py +++ b/test/python/qasm2/test_export.py @@ -44,9 +44,12 @@ def test_basic_output(self): qc.barrier(qr2) qc.cx(qr2[1], qr1[0]) qc.h(qr2[1]) - qc.x(qr2[1]).c_if(cr, 0) - qc.y(qr1[0]).c_if(cr, 1) - qc.z(qr1[0]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + qc.x(qr2[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.y(qr1[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.z(qr1[0]).c_if(cr, 2) qc.barrier(qr1, qr2) qc.measure(qr1[0], cr[0]) qc.measure(qr2[0], cr[1]) @@ -616,7 +619,8 @@ def test_rotation_angles_close_to_pi(self): def test_raises_on_single_bit_condition(self): qc = QuantumCircuit(1, 1) - qc.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, True) with self.assertRaisesRegex( qasm2.QASM2ExportError, "OpenQASM 2 can only condition on registers" diff --git a/test/python/qasm2/test_structure.py b/test/python/qasm2/test_structure.py index d963eea7e255..ffdfa907659c 100644 --- a/test/python/qasm2/test_structure.py +++ b/test/python/qasm2/test_structure.py @@ -256,11 +256,14 @@ def test_conditioned(self): if (cond == 0) U(0, 0, 0) q[0]; if (cond == 1) CX q[1], q[0]; """ - parsed = qiskit.qasm2.loads(program) + with self.assertWarns(DeprecationWarning): + parsed = qiskit.qasm2.loads(program) cond = ClassicalRegister(1, "cond") qc = QuantumCircuit(QuantumRegister(2, "q"), cond) - qc.u(0, 0, 0, 0).c_if(cond, 0) - qc.cx(1, 0).c_if(cond, 1) + with self.assertWarns(DeprecationWarning): + qc.u(0, 0, 0, 0).c_if(cond, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(1, 0).c_if(cond, 1) self.assertEqual(parsed, qc) def test_conditioned_broadcast(self): @@ -271,15 +274,20 @@ def test_conditioned_broadcast(self): if (cond == 0) U(0, 0, 0) q1; if (cond == 1) CX q1[0], q2; """ - parsed = qiskit.qasm2.loads(program) + with self.assertWarns(DeprecationWarning): + parsed = qiskit.qasm2.loads(program) cond = ClassicalRegister(1, "cond") q1 = QuantumRegister(2, "q1") q2 = QuantumRegister(2, "q2") qc = QuantumCircuit(q1, q2, cond) - qc.u(0, 0, 0, q1[0]).c_if(cond, 0) - qc.u(0, 0, 0, q1[1]).c_if(cond, 0) - qc.cx(q1[0], q2[0]).c_if(cond, 1) - qc.cx(q1[0], q2[1]).c_if(cond, 1) + with self.assertWarns(DeprecationWarning): + qc.u(0, 0, 0, q1[0]).c_if(cond, 0) + with self.assertWarns(DeprecationWarning): + qc.u(0, 0, 0, q1[1]).c_if(cond, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(q1[0], q2[0]).c_if(cond, 1) + with self.assertWarns(DeprecationWarning): + qc.cx(q1[0], q2[1]).c_if(cond, 1) self.assertEqual(parsed, qc) def test_constant_folding(self): @@ -338,19 +346,29 @@ def test_huge_conditions(self): if (cond=={bigint}) measure qr[0] -> cr[0]; if (cond=={bigint}) measure qr -> cr; """ - parsed = qiskit.qasm2.loads(program) + with self.assertWarns(DeprecationWarning): + parsed = qiskit.qasm2.loads(program) qr, cr = QuantumRegister(2, "qr"), ClassicalRegister(2, "cr") cond = ClassicalRegister(500, "cond") qc = QuantumCircuit(qr, cr, cond) - qc.u(0, 0, 0, qr[0]).c_if(cond, bigint) - qc.u(0, 0, 0, qr[0]).c_if(cond, bigint) - qc.u(0, 0, 0, qr[1]).c_if(cond, bigint) - qc.reset(qr[0]).c_if(cond, bigint) - qc.reset(qr[0]).c_if(cond, bigint) - qc.reset(qr[1]).c_if(cond, bigint) - qc.measure(qr[0], cr[0]).c_if(cond, bigint) - qc.measure(qr[0], cr[0]).c_if(cond, bigint) - qc.measure(qr[1], cr[1]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.u(0, 0, 0, qr[0]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.u(0, 0, 0, qr[0]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.u(0, 0, 0, qr[1]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.reset(qr[0]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.reset(qr[0]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.reset(qr[1]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.measure(qr[0], cr[0]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.measure(qr[0], cr[0]).c_if(cond, bigint) + with self.assertWarns(DeprecationWarning): + qc.measure(qr[1], cr[1]).c_if(cond, bigint) self.assertEqual(parsed, qc) @@ -383,14 +401,16 @@ def test_conditioned(self): creg cond[1]; if (cond == 0) not_bell q[0], q[1]; """ - parsed = qiskit.qasm2.loads(program) + with self.assertWarns(DeprecationWarning): + parsed = qiskit.qasm2.loads(program) not_bell_def = QuantumCircuit([Qubit(), Qubit()]) not_bell_def.u(0, 0, 0, 0) not_bell_def.cx(0, 1) not_bell = gate_builder("not_bell", [], not_bell_def) cond = ClassicalRegister(1, "cond") qc = QuantumCircuit(QuantumRegister(2, "q"), cond) - qc.append(not_bell().c_if(cond, 0), [0, 1]) + with self.assertWarns(DeprecationWarning): + qc.append(not_bell().c_if(cond, 0), [0, 1]) self.assertEqual(parsed, qc) def test_constant_folding_in_definition(self): @@ -735,7 +755,8 @@ def test_deepcopy_conditioned_defined_gate(self): creg c[1]; if (c == 1) my_gate q[0]; """ - parsed = qiskit.qasm2.loads(program) + with self.assertWarns(DeprecationWarning): + parsed = qiskit.qasm2.loads(program) my_gate = parsed.data[0].operation self.assertEqual(my_gate.name, "my_gate") @@ -904,12 +925,16 @@ def test_conditioned(self): if (cond == 0) measure q[0] -> c[0]; if (cond == 1) measure q -> c; """ - parsed = qiskit.qasm2.loads(program) + with self.assertWarns(DeprecationWarning): + parsed = qiskit.qasm2.loads(program) cond = ClassicalRegister(1, "cond") qc = QuantumCircuit(QuantumRegister(2, "q"), ClassicalRegister(2, "c"), cond) - qc.measure(0, 0).c_if(cond, 0) - qc.measure(0, 0).c_if(cond, 1) - qc.measure(1, 1).c_if(cond, 1) + with self.assertWarns(DeprecationWarning): + qc.measure(0, 0).c_if(cond, 0) + with self.assertWarns(DeprecationWarning): + qc.measure(0, 0).c_if(cond, 1) + with self.assertWarns(DeprecationWarning): + qc.measure(1, 1).c_if(cond, 1) self.assertEqual(parsed, qc) def test_broadcast_against_empty_register(self): @@ -991,12 +1016,16 @@ def test_conditioned(self): if (cond == 0) reset q[0]; if (cond == 1) reset q; """ - parsed = qiskit.qasm2.loads(program) + with self.assertWarns(DeprecationWarning): + parsed = qiskit.qasm2.loads(program) cond = ClassicalRegister(1, "cond") qc = QuantumCircuit(QuantumRegister(2, "q"), cond) - qc.reset(0).c_if(cond, 0) - qc.reset(0).c_if(cond, 1) - qc.reset(1).c_if(cond, 1) + with self.assertWarns(DeprecationWarning): + qc.reset(0).c_if(cond, 0) + with self.assertWarns(DeprecationWarning): + qc.reset(0).c_if(cond, 1) + with self.assertWarns(DeprecationWarning): + qc.reset(1).c_if(cond, 1) self.assertEqual(parsed, qc) def test_broadcast_against_empty_register(self): diff --git a/test/python/qasm3/test_export.py b/test/python/qasm3/test_export.py index eefc0a2cc53c..acae1d7d3cd7 100644 --- a/test/python/qasm3/test_export.py +++ b/test/python/qasm3/test_export.py @@ -104,9 +104,12 @@ def test_regs_conds_qasm(self): qc.measure(qr1[0], cr[0]) qc.measure(qr2[0], cr[1]) qc.measure(qr2[1], cr[2]) - qc.x(qr2[1]).c_if(cr, 0) - qc.y(qr1[0]).c_if(cr, 1) - qc.z(qr1[0]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + qc.x(qr2[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.y(qr1[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.z(qr1[0]).c_if(cr, 2) expected_qasm = "\n".join( [ "OPENQASM 3.0;", @@ -723,8 +726,10 @@ def test_teleportation(self): qc.barrier() qc.measure([0, 1], [0, 1]) qc.barrier() - qc.x(2).c_if(qc.clbits[1], 1) - qc.z(2).c_if(qc.clbits[0], 1) + with self.assertWarns(DeprecationWarning): + qc.x(2).c_if(qc.clbits[1], 1) + with self.assertWarns(DeprecationWarning): + qc.z(2).c_if(qc.clbits[0], 1) transpiled = transpile(qc, initial_layout=[0, 1, 2]) expected_qasm = """\ @@ -780,8 +785,10 @@ def test_basis_gates(self): qc.barrier() qc.measure([0, 1], [0, 1]) qc.barrier() - qc.x(2).c_if(qc.clbits[1], 1) - qc.z(2).c_if(qc.clbits[0], 1) + with self.assertWarns(DeprecationWarning): + qc.x(2).c_if(qc.clbits[1], 1) + with self.assertWarns(DeprecationWarning): + qc.z(2).c_if(qc.clbits[0], 1) transpiled = transpile(qc, initial_layout=[0, 1, 2]) expected_qasm = """\ @@ -2046,11 +2053,15 @@ def test_unusual_conditions(self): qc = QuantumCircuit(3, 2) qc.h(0) qc.measure(0, 0) - qc.measure(1, 1).c_if(0, True) - qc.reset([0, 1]).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.measure(1, 1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.reset([0, 1]).c_if(0, True) with qc.while_loop((qc.clbits[0], True)): - qc.break_loop().c_if(0, True) - qc.continue_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.break_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.continue_loop().c_if(0, True) # Terra forbids delay and barrier from being conditioned through `c_if`, but in theory they # should work fine in a dynamic-circuits sense (although what a conditional barrier _means_ # is a whole other kettle of fish). diff --git a/test/python/quantum_info/operators/symplectic/test_clifford.py b/test/python/quantum_info/operators/symplectic/test_clifford.py index e78feaf3b78f..253bc15852b6 100644 --- a/test/python/quantum_info/operators/symplectic/test_clifford.py +++ b/test/python/quantum_info/operators/symplectic/test_clifford.py @@ -387,7 +387,8 @@ def test_barrier_delay_sim(self): def test_from_circuit_with_conditional_gate(self): """Test initialization from circuit with conditional gate.""" qc = QuantumCircuit(2, 1) - qc.h(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.h(0).c_if(0, 0) qc.cx(0, 1) with self.assertRaises(QiskitError): diff --git a/test/python/transpiler/legacy_scheduling/test_instruction_alignments.py b/test/python/transpiler/legacy_scheduling/test_instruction_alignments.py index c95d65422d4b..38b52913160e 100644 --- a/test/python/transpiler/legacy_scheduling/test_instruction_alignments.py +++ b/test/python/transpiler/legacy_scheduling/test_instruction_alignments.py @@ -304,7 +304,8 @@ def test_circuit_using_clbit(self): circuit.x(0) circuit.delay(100, 0, unit="dt") circuit.measure(0, 0) - circuit.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + circuit.x(1).c_if(0, 1) circuit.measure(2, 0) timed_circuit = self.time_conversion_pass(circuit) @@ -320,7 +321,8 @@ def test_circuit_using_clbit(self): ref_circuit.delay(1872, 1, unit="dt") # 2032 - 160 ref_circuit.delay(432, 2, unit="dt") # 2032 - 1600 ref_circuit.measure(0, 0) - ref_circuit.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.x(1).c_if(0, 1) ref_circuit.delay(160, 0, unit="dt") ref_circuit.measure(2, 0) diff --git a/test/python/transpiler/legacy_scheduling/test_scheduling_pass.py b/test/python/transpiler/legacy_scheduling/test_scheduling_pass.py index 417ff9b42212..a13ca2e12de1 100644 --- a/test/python/transpiler/legacy_scheduling/test_scheduling_pass.py +++ b/test/python/transpiler/legacy_scheduling/test_scheduling_pass.py @@ -82,7 +82,8 @@ def test_classically_controlled_gate_after_measure(self, schedule_pass): """ qc = QuantumCircuit(2, 1) qc.measure(0, 0) - qc.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, True) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) with self.assertWarns(DeprecationWarning): @@ -92,7 +93,8 @@ def test_classically_controlled_gate_after_measure(self, schedule_pass): expected = QuantumCircuit(2, 1) expected.measure(0, 0) expected.delay(1000, 1) # x.c_if starts after measure - expected.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, True) expected.delay(200, 0) self.assertEqual(expected, scheduled) @@ -170,8 +172,10 @@ def test_c_if_on_different_qubits(self, schedule_pass): """ qc = QuantumCircuit(3, 1) qc.measure(0, 0) - qc.x(1).c_if(0, True) - qc.x(2).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(2).c_if(0, True) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) with self.assertWarns(DeprecationWarning): @@ -182,8 +186,10 @@ def test_c_if_on_different_qubits(self, schedule_pass): expected.measure(0, 0) expected.delay(1000, 1) expected.delay(1000, 2) - expected.x(1).c_if(0, True) - expected.x(2).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(2).c_if(0, True) expected.delay(200, 0) self.assertEqual(expected, scheduled) @@ -255,7 +261,8 @@ def test_measure_after_c_if(self, schedule_pass): """ qc = QuantumCircuit(3, 1) qc.measure(0, 0) - qc.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 1) qc.measure(2, 0) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) @@ -267,7 +274,8 @@ def test_measure_after_c_if(self, schedule_pass): expected.delay(1000, 1) expected.delay(1000, 2) expected.measure(0, 0) - expected.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, 1) expected.measure(2, 0) expected.delay(1000, 0) expected.delay(800, 1) @@ -451,7 +459,8 @@ def test_measure_after_c_if_on_edge_locking(self): """ qc = QuantumCircuit(3, 1) qc.measure(0, 0) - qc.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 1) qc.measure(2, 0) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) @@ -465,7 +474,8 @@ def test_measure_after_c_if_on_edge_locking(self): expected_asap = QuantumCircuit(3, 1) expected_asap.measure(0, 0) expected_asap.delay(1000, 1) - expected_asap.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_asap.x(1).c_if(0, 1) expected_asap.measure(2, 0) expected_asap.delay(200, 0) expected_asap.delay(200, 2) @@ -474,7 +484,8 @@ def test_measure_after_c_if_on_edge_locking(self): expected_alap = QuantumCircuit(3, 1) expected_alap.measure(0, 0) expected_alap.delay(1000, 1) - expected_alap.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_alap.x(1).c_if(0, 1) expected_alap.delay(200, 2) expected_alap.measure(2, 0) expected_alap.delay(200, 0) @@ -500,11 +511,14 @@ def test_active_reset_circuit(self, write_lat, cond_lat): """ qc = QuantumCircuit(1, 1) qc.measure(0, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) qc.measure(0, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) qc.measure(0, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) durations = InstructionDurations([("x", None, 100), ("measure", None, 1000)]) with self.assertWarns(DeprecationWarning): @@ -519,15 +533,18 @@ def test_active_reset_circuit(self, write_lat, cond_lat): expected.measure(0, 0) if cond_lat > 0: expected.delay(cond_lat, 0) - expected.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, 1) expected.measure(0, 0) if cond_lat > 0: expected.delay(cond_lat, 0) - expected.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, 1) expected.measure(0, 0) if cond_lat > 0: expected.delay(cond_lat, 0) - expected.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, 1) self.assertEqual(expected, actual_asap) self.assertEqual(expected, actual_alap) @@ -616,15 +633,19 @@ def test_random_complicated_circuit(self): """ qc = QuantumCircuit(3, 1) qc.delay(100, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) qc.barrier() qc.measure(2, 0) - qc.x(1).c_if(0, 0) - qc.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 0) qc.delay(300, 0) qc.cx(1, 2) qc.x(0) - qc.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(0, 0) qc.measure(2, 0) durations = InstructionDurations( @@ -644,19 +665,23 @@ def test_random_complicated_circuit(self): expected_asap.delay(100, 0) # due to conditional latency of 200dt expected_asap.delay(300, 1) expected_asap.delay(300, 2) - expected_asap.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_asap.x(0).c_if(0, 1) expected_asap.barrier() expected_asap.delay(1400, 0) expected_asap.delay(1200, 1) expected_asap.measure(2, 0) - expected_asap.x(1).c_if(0, 0) - expected_asap.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_asap.x(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_asap.x(0).c_if(0, 0) expected_asap.delay(300, 0) expected_asap.x(0) expected_asap.delay(300, 2) expected_asap.cx(1, 2) expected_asap.delay(400, 1) - expected_asap.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_asap.cx(0, 1).c_if(0, 0) expected_asap.delay(700, 0) # creg is released at t0 of cx(0,1).c_if(0,0) expected_asap.delay( 700, 1 @@ -671,20 +696,24 @@ def test_random_complicated_circuit(self): expected_alap.delay(100, 0) # due to conditional latency of 200dt expected_alap.delay(300, 1) expected_alap.delay(300, 2) - expected_alap.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_alap.x(0).c_if(0, 1) expected_alap.barrier() expected_alap.delay(1400, 0) expected_alap.delay(1200, 1) expected_alap.measure(2, 0) - expected_alap.x(1).c_if(0, 0) - expected_alap.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_alap.x(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_alap.x(0).c_if(0, 0) expected_alap.delay(300, 0) expected_alap.x(0) expected_alap.delay(300, 1) expected_alap.delay(600, 2) expected_alap.cx(1, 2) expected_alap.delay(100, 1) - expected_alap.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_alap.cx(0, 1).c_if(0, 0) expected_alap.measure(2, 0) expected_alap.delay(700, 0) expected_alap.delay(700, 1) @@ -722,8 +751,10 @@ def test_dag_introduces_extra_dependency_between_conditionals(self): """ qc = QuantumCircuit(2, 1) qc.delay(100, 0) - qc.x(0).c_if(0, True) - qc.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, True) durations = InstructionDurations([("x", None, 160)]) with self.assertWarns(DeprecationWarning): @@ -733,8 +764,10 @@ def test_dag_introduces_extra_dependency_between_conditionals(self): expected = QuantumCircuit(2, 1) expected.delay(100, 0) expected.delay(100, 1) # due to extra dependency on clbits - expected.x(0).c_if(0, True) - expected.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, True) self.assertEqual(expected, scheduled) diff --git a/test/python/transpiler/test_barrier_before_final_measurements.py b/test/python/transpiler/test_barrier_before_final_measurements.py index eb4f509e5301..3aaa5c7895cd 100644 --- a/test/python/transpiler/test_barrier_before_final_measurements.py +++ b/test/python/transpiler/test_barrier_before_final_measurements.py @@ -175,13 +175,15 @@ def test_preserve_measure_for_conditional(self): circuit.h(qr0) circuit.measure(qr0, cr0) - circuit.z(qr1).c_if(cr0, 1) + with self.assertWarns(DeprecationWarning): + circuit.z(qr1).c_if(cr0, 1) circuit.measure(qr1, cr1) expected = QuantumCircuit(qr0, qr1, cr0, cr1) expected.h(qr0) expected.measure(qr0, cr0) - expected.z(qr1).c_if(cr0, 1) + with self.assertWarns(DeprecationWarning): + expected.z(qr1).c_if(cr0, 1) expected.barrier(qr0, qr1) expected.measure(qr1, cr1) @@ -377,17 +379,23 @@ def test_conditioned_on_single_bit(self): circuit = QuantumCircuit(QuantumRegister(3), ClassicalRegister(2), [Clbit()]) circuit.h(range(3)) circuit.measure(range(3), range(3)) - circuit.h(0).c_if(circuit.cregs[0], 3) - circuit.h(1).c_if(circuit.clbits[-1], True) - circuit.h(2).c_if(circuit.clbits[-1], False) + with self.assertWarns(DeprecationWarning): + circuit.h(0).c_if(circuit.cregs[0], 3) + with self.assertWarns(DeprecationWarning): + circuit.h(1).c_if(circuit.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + circuit.h(2).c_if(circuit.clbits[-1], False) circuit.measure(range(3), range(3)) expected = circuit.copy_empty_like() expected.h(range(3)) expected.measure(range(3), range(3)) - expected.h(0).c_if(expected.cregs[0], 3) - expected.h(1).c_if(expected.clbits[-1], True) - expected.h(2).c_if(expected.clbits[-1], False) + with self.assertWarns(DeprecationWarning): + expected.h(0).c_if(expected.cregs[0], 3) + with self.assertWarns(DeprecationWarning): + expected.h(1).c_if(expected.clbits[-1], True) + with self.assertWarns(DeprecationWarning): + expected.h(2).c_if(expected.clbits[-1], False) expected.barrier(range(3)) expected.measure(range(3), range(3)) diff --git a/test/python/transpiler/test_basis_translator.py b/test/python/transpiler/test_basis_translator.py index 9dae6c3f283a..820c4f241a1e 100644 --- a/test/python/transpiler/test_basis_translator.py +++ b/test/python/transpiler/test_basis_translator.py @@ -576,9 +576,12 @@ def test_unroll_1q_chain_conditional(self): circuit.rz(0.3, qr) circuit.rx(0.1, qr) circuit.measure(qr, cr) - circuit.x(qr).c_if(cr, 1) - circuit.y(qr).c_if(cr, 1) - circuit.z(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.x(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.y(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.z(qr).c_if(cr, 1) dag = circuit_to_dag(circuit) pass_ = UnrollCustomDefinitions(std_eqlib, ["u1", "u2", "u3"]) dag = pass_.run(dag) @@ -609,9 +612,12 @@ def test_unroll_1q_chain_conditional(self): ref_circuit.append(U1Gate(0.3), [qr[0]]) ref_circuit.append(U3Gate(0.1, -pi / 2, pi / 2), [qr[0]]) ref_circuit.measure(qr[0], cr[0]) - ref_circuit.append(U3Gate(pi, 0, pi), [qr[0]]).c_if(cr, 1) - ref_circuit.append(U3Gate(pi, pi / 2, pi / 2), [qr[0]]).c_if(cr, 1) - ref_circuit.append(U1Gate(pi), [qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.append(U3Gate(pi, 0, pi), [qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.append(U3Gate(pi, pi / 2, pi / 2), [qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.append(U1Gate(pi), [qr[0]]).c_if(cr, 1) ref_dag = circuit_to_dag(ref_circuit) self.assertEqual(unrolled_dag, ref_dag) @@ -1073,7 +1079,8 @@ def test_condition_set_substitute_node(self): circ.h(0) circ.cx(0, 1) circ.measure(1, 1) - circ.h(0).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circ.h(0).c_if(cr, 1) circ_transpiled = transpile(circ, optimization_level=3, basis_gates=["cx", "id", "u"]) # ┌────────────┐ ┌────────────┐ @@ -1089,7 +1096,8 @@ def test_condition_set_substitute_node(self): expected.u(pi / 2, 0, pi, 0) expected.cx(0, 1) expected.measure(1, 1) - expected.u(pi / 2, 0, pi, 0).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + expected.u(pi / 2, 0, pi, 0).c_if(cr, 1) self.assertEqual(circ_transpiled, expected) diff --git a/test/python/transpiler/test_check_map.py b/test/python/transpiler/test_check_map.py index 2a88b272c74d..866eb9f4dfae 100644 --- a/test/python/transpiler/test_check_map.py +++ b/test/python/transpiler/test_check_map.py @@ -268,7 +268,8 @@ def test_nested_conditional_unusual_bit_order(self): # should all be fine. This kind of thing is a staple of the control-flow builders. inner_order = [cr2[0], cr1[0], cr2[1], cr1[1]] inner = QuantumCircuit(qr, inner_order, cr1, cr2) - inner.cx(0, 1).c_if(cr2, 3) + with self.assertWarns(DeprecationWarning): + inner.cx(0, 1).c_if(cr2, 3) outer = QuantumCircuit(qr, cr1, cr2) outer.if_test((cr1, 3), inner, outer.qubits, inner_order) diff --git a/test/python/transpiler/test_clifford_passes.py b/test/python/transpiler/test_clifford_passes.py index ff8be63ffbcd..08bbbf4cb884 100644 --- a/test/python/transpiler/test_clifford_passes.py +++ b/test/python/transpiler/test_clifford_passes.py @@ -620,7 +620,8 @@ def test_do_not_merge_conditional_gates(self): qc.cx(1, 0) qc.x(0) qc.x(1) - qc.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 1) qc.x(0) qc.x(1) qc.cx(0, 1) diff --git a/test/python/transpiler/test_collect_2q_blocks.py b/test/python/transpiler/test_collect_2q_blocks.py index 69efc210a9f6..362765101f08 100644 --- a/test/python/transpiler/test_collect_2q_blocks.py +++ b/test/python/transpiler/test_collect_2q_blocks.py @@ -129,11 +129,15 @@ def test_do_not_merge_conditioned_gates(self): qc = QuantumCircuit(qr, cr) qc.p(0.1, 0) - qc.p(0.2, 0).c_if(cr, 0) - qc.p(0.3, 0).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.p(0.2, 0).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.p(0.3, 0).c_if(cr, 0) qc.cx(0, 1) - qc.cx(1, 0).c_if(cr, 0) - qc.cx(0, 1).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.cx(1, 0).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(cr, 1) pass_manager = PassManager() pass_manager.append(Collect2qBlocks()) diff --git a/test/python/transpiler/test_collect_multiq_blocks.py b/test/python/transpiler/test_collect_multiq_blocks.py index e2446d03da2a..2d4bc8783764 100644 --- a/test/python/transpiler/test_collect_multiq_blocks.py +++ b/test/python/transpiler/test_collect_multiq_blocks.py @@ -127,7 +127,8 @@ def test_block_with_classical_register(self): if(c0==0) u1(0.25*pi) q[1]; if(c0==0) u2(0.25*pi, 0.25*pi) q[0]; """ - qc = QuantumCircuit.from_qasm_str(qasmstr) + with self.assertWarns(DeprecationWarning): + qc = QuantumCircuit.from_qasm_str(qasmstr) pass_manager = PassManager() pass_manager.append(CollectMultiQBlocks()) @@ -166,11 +167,15 @@ def test_do_not_merge_conditioned_gates(self): qc = QuantumCircuit(qr, cr) qc.p(0.1, 0) - qc.p(0.2, 0).c_if(cr, 0) - qc.p(0.3, 0).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.p(0.2, 0).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.p(0.3, 0).c_if(cr, 0) qc.cx(0, 1) - qc.cx(1, 0).c_if(cr, 0) - qc.cx(0, 1).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.cx(1, 0).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(cr, 1) pass_manager = PassManager() pass_manager.append(CollectMultiQBlocks()) diff --git a/test/python/transpiler/test_commutative_cancellation.py b/test/python/transpiler/test_commutative_cancellation.py index 88f1d99bef31..15b7f6705787 100644 --- a/test/python/transpiler/test_commutative_cancellation.py +++ b/test/python/transpiler/test_commutative_cancellation.py @@ -564,7 +564,8 @@ def test_conditional_gates_dont_commute(self): circuit.h(0) circuit.measure(0, 0) circuit.cx(1, 2) - circuit.cx(1, 2).c_if(circuit.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + circuit.cx(1, 2).c_if(circuit.cregs[0], 0) circuit.measure([1, 2], [0, 1]) new_pm = PassManager(CommutativeCancellation()) @@ -677,8 +678,10 @@ def test_basic_classical_wires(self): """Test that transpile runs without internal errors when dealing with commutable operations with classical controls. Regression test for gh-8553.""" original = QuantumCircuit(2, 1) - original.x(0).c_if(original.cregs[0], 0) - original.x(1).c_if(original.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + original.x(0).c_if(original.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + original.x(1).c_if(original.cregs[0], 0) # This transpilation shouldn't change anything, but it should succeed. At one point it was # triggering an internal logic error and crashing. transpiled = PassManager([CommutativeCancellation()]).run(original) diff --git a/test/python/transpiler/test_commutative_inverse_cancellation.py b/test/python/transpiler/test_commutative_inverse_cancellation.py index cd800a3bb46f..03492b8a8c2a 100644 --- a/test/python/transpiler/test_commutative_inverse_cancellation.py +++ b/test/python/transpiler/test_commutative_inverse_cancellation.py @@ -414,7 +414,8 @@ def test_conditional_gates_dont_commute(self, matrix_based): circuit.h(0) circuit.measure(0, 0) circuit.cx(1, 2) - circuit.cx(1, 2).c_if(circuit.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + circuit.cx(1, 2).c_if(circuit.cregs[0], 0) circuit.measure([1, 2], [0, 1]) passmanager = PassManager(CommutativeInverseCancellation(matrix_based=matrix_based)) diff --git a/test/python/transpiler/test_consolidate_blocks.py b/test/python/transpiler/test_consolidate_blocks.py index 1984ad1a3dc4..3b3aff1db228 100644 --- a/test/python/transpiler/test_consolidate_blocks.py +++ b/test/python/transpiler/test_consolidate_blocks.py @@ -328,7 +328,8 @@ def test_classical_conditions_maintained(self): This issue was raised in #2752 """ qc = QuantumCircuit(1, 1) - qc.h(0).c_if(qc.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc.h(0).c_if(qc.cregs[0], 1) qc.measure(0, 0) pass_manager = PassManager() diff --git a/test/python/transpiler/test_convert_conditions_to_if_ops.py b/test/python/transpiler/test_convert_conditions_to_if_ops.py index 799a71163590..d1eadda0d92d 100644 --- a/test/python/transpiler/test_convert_conditions_to_if_ops.py +++ b/test/python/transpiler/test_convert_conditions_to_if_ops.py @@ -26,13 +26,17 @@ def test_simple_loose_bits(self): base = QuantumCircuit(bits) base.h(0) - base.x(0).c_if(0, 1) - base.z(1).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + base.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + base.z(1).c_if(1, 0) base.measure(0, 0) base.measure(1, 1) base.h(0) - base.x(0).c_if(0, 1) - base.cx(0, 1).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + base.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + base.cx(0, 1).c_if(1, 0) expected = QuantumCircuit(bits) expected.h(0) @@ -48,8 +52,8 @@ def test_simple_loose_bits(self): with expected.if_test((expected.clbits[1], False)): expected.cx(0, 1) expected = canonicalize_control_flow(expected) - - output = PassManager([ConvertConditionsToIfOps()]).run(base) + with self.assertWarns(DeprecationWarning): + output = PassManager([ConvertConditionsToIfOps()]).run(base) self.assertEqual(output, expected) def test_simple_registers(self): @@ -58,13 +62,17 @@ def test_simple_registers(self): base = QuantumCircuit(*registers) base.h(0) - base.x(0).c_if(base.cregs[0], 1) - base.z(1).c_if(base.cregs[1], 0) + with self.assertWarns(DeprecationWarning): + base.x(0).c_if(base.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + base.z(1).c_if(base.cregs[1], 0) base.measure(0, 0) base.measure(1, 2) base.h(0) - base.x(0).c_if(base.cregs[0], 1) - base.cx(0, 1).c_if(base.cregs[1], 0) + with self.assertWarns(DeprecationWarning): + base.x(0).c_if(base.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + base.cx(0, 1).c_if(base.cregs[1], 0) expected = QuantumCircuit(*registers) expected.h(0) @@ -81,7 +89,8 @@ def test_simple_registers(self): expected.cx(0, 1) expected = canonicalize_control_flow(expected) - output = PassManager([ConvertConditionsToIfOps()]).run(base) + with self.assertWarns(DeprecationWarning): + output = PassManager([ConvertConditionsToIfOps()]).run(base) self.assertEqual(output, expected) def test_nested_control_flow(self): @@ -91,14 +100,18 @@ def test_nested_control_flow(self): registers = [QuantumRegister(3), ClassicalRegister(2)] base = QuantumCircuit(*registers, bits) - base.x(0).c_if(bits[0], False) + with self.assertWarns(DeprecationWarning): + base.x(0).c_if(bits[0], False) with base.if_test((base.cregs[0], 0)) as else_: - base.z(1).c_if(bits[0], False) + with self.assertWarns(DeprecationWarning): + base.z(1).c_if(bits[0], False) with else_: - base.z(1).c_if(base.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + base.z(1).c_if(base.cregs[0], 1) with base.for_loop(range(2)): with base.while_loop((base.cregs[0], 1)): - base.cx(1, 2).c_if(base.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + base.cx(1, 2).c_if(base.cregs[0], 1) base = canonicalize_control_flow(base) expected = QuantumCircuit(*registers, bits) @@ -115,8 +128,8 @@ def test_nested_control_flow(self): with expected.if_test((expected.cregs[0], 1)): expected.cx(1, 2) expected = canonicalize_control_flow(expected) - - output = PassManager([ConvertConditionsToIfOps()]).run(base) + with self.assertWarns(DeprecationWarning): + output = PassManager([ConvertConditionsToIfOps()]).run(base) self.assertEqual(output, expected) def test_no_op(self): @@ -135,5 +148,6 @@ def test_no_op(self): with base.while_loop((base.cregs[0], 1)): base.cx(1, 2) base = canonicalize_control_flow(base) - output = PassManager([ConvertConditionsToIfOps()]).run(base) + with self.assertWarns(DeprecationWarning): + output = PassManager([ConvertConditionsToIfOps()]).run(base) self.assertEqual(output, base) diff --git a/test/python/transpiler/test_decompose.py b/test/python/transpiler/test_decompose.py index 1223b37ca3ff..d77e63303f40 100644 --- a/test/python/transpiler/test_decompose.py +++ b/test/python/transpiler/test_decompose.py @@ -117,15 +117,19 @@ def test_decompose_conditional(self): qr = QuantumRegister(1, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr).c_if(cr, 1) - circuit.x(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.x(qr).c_if(cr, 1) dag = circuit_to_dag(circuit) pass_ = Decompose(HGate) after_dag = pass_.run(dag) ref_circuit = QuantumCircuit(qr, cr) - ref_circuit.append(U2Gate(0, pi), [qr[0]]).c_if(cr, 1) - ref_circuit.x(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.append(U2Gate(0, pi), [qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.x(qr).c_if(cr, 1) ref_dag = circuit_to_dag(ref_circuit) self.assertEqual(after_dag, ref_dag) diff --git a/test/python/transpiler/test_elide_permutations.py b/test/python/transpiler/test_elide_permutations.py index 6f3051cec0d6..edef139af2ee 100644 --- a/test/python/transpiler/test_elide_permutations.py +++ b/test/python/transpiler/test_elide_permutations.py @@ -180,7 +180,8 @@ def test_swap_condition(self): """Test swap elision doesn't touch conditioned swap.""" qc = QuantumCircuit(3, 3) qc.h(0) - qc.swap(0, 1).c_if(qc.clbits[0], 0) + with self.assertWarns(DeprecationWarning): + qc.swap(0, 1).c_if(qc.clbits[0], 0) qc.cx(0, 1) res = self.swap_pass(qc) self.assertEqual(res, qc) diff --git a/test/python/transpiler/test_gate_direction.py b/test/python/transpiler/test_gate_direction.py index 569a210f8a9d..9bdc085a5b5e 100644 --- a/test/python/transpiler/test_gate_direction.py +++ b/test/python/transpiler/test_gate_direction.py @@ -243,8 +243,10 @@ def test_preserves_conditions(self): cr = ClassicalRegister(1, "c") circuit = QuantumCircuit(qr, cr) - circuit.cx(qr[0], qr[1]).c_if(cr, 0) - circuit.cx(qr[1], qr[0]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + circuit.cx(qr[0], qr[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + circuit.cx(qr[1], qr[0]).c_if(cr, 0) circuit.cx(qr[0], qr[1]) circuit.cx(qr[1], qr[0]) @@ -261,16 +263,22 @@ def test_preserves_conditions(self): # c: 1/╡ 0x0 ╞╡ 0x0 ╞╡ 0x0 ╞╡ 0x0 ╞╡ 0x0 ╞╡ 0x0 ╞════════════════════ # └─────┘└─────┘└─────┘└─────┘└─────┘└─────┘ expected = QuantumCircuit(qr, cr) - expected.cx(qr[0], qr[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + expected.cx(qr[0], qr[1]).c_if(cr, 0) # Order of H gates is important because DAG comparison will consider # different conditional order on a creg to be a different circuit. # See https://github.com/Qiskit/qiskit-terra/issues/3164 - expected.h(qr[1]).c_if(cr, 0) - expected.h(qr[0]).c_if(cr, 0) - expected.cx(qr[0], qr[1]).c_if(cr, 0) - expected.h(qr[1]).c_if(cr, 0) - expected.h(qr[0]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + expected.h(qr[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + expected.h(qr[0]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + expected.cx(qr[0], qr[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + expected.h(qr[1]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + expected.h(qr[0]).c_if(cr, 0) expected.cx(qr[0], qr[1]) expected.h(qr[1]) diff --git a/test/python/transpiler/test_high_level_synthesis.py b/test/python/transpiler/test_high_level_synthesis.py index a7dd806e63e9..973035a399c9 100644 --- a/test/python/transpiler/test_high_level_synthesis.py +++ b/test/python/transpiler/test_high_level_synthesis.py @@ -1588,9 +1588,12 @@ def test_unroll_1q_chain_conditional(self): circuit.rz(0.3, qr) circuit.rx(0.1, qr) circuit.measure(qr, cr) - circuit.x(qr).c_if(cr, 1) - circuit.y(qr).c_if(cr, 1) - circuit.z(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.x(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.y(qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.z(qr).c_if(cr, 1) dag = circuit_to_dag(circuit) pass_ = HighLevelSynthesis(equivalence_library=std_eqlib, basis_gates=["u1", "u2", "u3"]) dag = pass_.run(dag) @@ -1621,9 +1624,12 @@ def test_unroll_1q_chain_conditional(self): ref_circuit.append(U1Gate(0.3), [qr[0]]) ref_circuit.append(U3Gate(0.1, -np.pi / 2, np.pi / 2), [qr[0]]) ref_circuit.measure(qr[0], cr[0]) - ref_circuit.append(U3Gate(np.pi, 0, np.pi), [qr[0]]).c_if(cr, 1) - ref_circuit.append(U3Gate(np.pi, np.pi / 2, np.pi / 2), [qr[0]]).c_if(cr, 1) - ref_circuit.append(U1Gate(np.pi), [qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.append(U3Gate(np.pi, 0, np.pi), [qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.append(U3Gate(np.pi, np.pi / 2, np.pi / 2), [qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + ref_circuit.append(U1Gate(np.pi), [qr[0]]).c_if(cr, 1) ref_dag = circuit_to_dag(ref_circuit) self.assertEqual(unrolled_dag, ref_dag) diff --git a/test/python/transpiler/test_linear_functions_passes.py b/test/python/transpiler/test_linear_functions_passes.py index 1d157c437155..85dccbfa53fa 100644 --- a/test/python/transpiler/test_linear_functions_passes.py +++ b/test/python/transpiler/test_linear_functions_passes.py @@ -615,7 +615,8 @@ def test_do_not_merge_conditional_gates(self): qc = QuantumCircuit(2, 1) qc.cx(1, 0) qc.swap(1, 0) - qc.cx(0, 1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(0, 1) qc.cx(0, 1) qc.cx(1, 0) diff --git a/test/python/transpiler/test_optimize_1q_decomposition.py b/test/python/transpiler/test_optimize_1q_decomposition.py index 06aab474d611..fb043ff9d950 100644 --- a/test/python/transpiler/test_optimize_1q_decomposition.py +++ b/test/python/transpiler/test_optimize_1q_decomposition.py @@ -212,16 +212,18 @@ def test_ignores_conditional_rotations(self, basis): qr = QuantumRegister(1, "qr") cr = ClassicalRegister(2, "cr") circuit = QuantumCircuit(qr, cr) - circuit.p(0.1, qr).c_if(cr, 1) - circuit.p(0.2, qr).c_if(cr, 3) + with self.assertWarns(DeprecationWarning): + circuit.p(0.1, qr).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.p(0.2, qr).c_if(cr, 3) circuit.p(0.3, qr) circuit.p(0.4, qr) passmanager = PassManager() passmanager.append(Optimize1qGatesDecomposition(basis)) result = passmanager.run(circuit) - - self.assertTrue(Operator(circuit).equiv(Operator(result))) + with self.assertWarns(DeprecationWarning): + self.assertTrue(Operator(circuit).equiv(Operator(result))) @ddt.data( ["cx", "u3"], diff --git a/test/python/transpiler/test_optimize_1q_gates.py b/test/python/transpiler/test_optimize_1q_gates.py index e5483dd47499..bfa578826510 100644 --- a/test/python/transpiler/test_optimize_1q_gates.py +++ b/test/python/transpiler/test_optimize_1q_gates.py @@ -162,15 +162,19 @@ def test_ignores_conditional_rotations(self): qr = QuantumRegister(1, "qr") cr = ClassicalRegister(2, "cr") circuit = QuantumCircuit(qr, cr) - circuit.append(U1Gate(0.1), [qr]).c_if(cr, 1) - circuit.append(U1Gate(0.2), [qr]).c_if(cr, 3) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0.1), [qr]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0.2), [qr]).c_if(cr, 3) circuit.append(U1Gate(0.3), [qr]) circuit.append(U1Gate(0.4), [qr]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr, cr) - expected.append(U1Gate(0.1), [qr]).c_if(cr, 1) - expected.append(U1Gate(0.2), [qr]).c_if(cr, 3) + with self.assertWarns(DeprecationWarning): + expected.append(U1Gate(0.1), [qr]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + expected.append(U1Gate(0.2), [qr]).c_if(cr, 3) expected.append(U1Gate(0.7), [qr]) pass_ = Optimize1qGates() @@ -190,15 +194,19 @@ def test_ignores_conditional_rotations_phase_gates(self): qr = QuantumRegister(1, "qr") cr = ClassicalRegister(2, "cr") circuit = QuantumCircuit(qr, cr) - circuit.append(PhaseGate(0.1), [qr]).c_if(cr, 1) - circuit.append(PhaseGate(0.2), [qr]).c_if(cr, 3) + with self.assertWarns(DeprecationWarning): + circuit.append(PhaseGate(0.1), [qr]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.append(PhaseGate(0.2), [qr]).c_if(cr, 3) circuit.append(PhaseGate(0.3), [qr]) circuit.append(PhaseGate(0.4), [qr]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr, cr) - expected.append(PhaseGate(0.1), [qr]).c_if(cr, 1) - expected.append(PhaseGate(0.2), [qr]).c_if(cr, 3) + with self.assertWarns(DeprecationWarning): + expected.append(PhaseGate(0.1), [qr]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + expected.append(PhaseGate(0.2), [qr]).c_if(cr, 3) expected.append(PhaseGate(0.7), [qr]) pass_ = Optimize1qGates(["p", "u2", "u", "cx", "id"]) diff --git a/test/python/transpiler/test_optimize_swap_before_measure.py b/test/python/transpiler/test_optimize_swap_before_measure.py index 6d7c06f410a7..10455318bd09 100644 --- a/test/python/transpiler/test_optimize_swap_before_measure.py +++ b/test/python/transpiler/test_optimize_swap_before_measure.py @@ -363,12 +363,14 @@ def test_no_optimize_swap_with_condition(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.swap(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.swap(qr[0], qr[1]).c_if(cr, 1) circuit.measure(qr[0], cr[0]) dag = circuit_to_dag(circuit) expected = QuantumCircuit(qr, cr) - expected.swap(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + expected.swap(qr[0], qr[1]).c_if(cr, 1) expected.measure(qr[0], cr[0]) pass_ = OptimizeSwapBeforeMeasure() diff --git a/test/python/transpiler/test_preset_passmanagers.py b/test/python/transpiler/test_preset_passmanagers.py index b762d84032bb..52c2cb06d90c 100644 --- a/test/python/transpiler/test_preset_passmanagers.py +++ b/test/python/transpiler/test_preset_passmanagers.py @@ -1214,7 +1214,8 @@ def test_optimization_condition(self, level): qr = QuantumRegister(2) cr = ClassicalRegister(1) qc = QuantumCircuit(qr, cr) - qc.cx(0, 1).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(cr, 1) backend = GenericBackendV2( num_qubits=20, coupling_map=TOKYO_CMAP, @@ -1227,7 +1228,8 @@ def test_optimization_condition(self, level): def test_input_dag_copy(self): """Test substitute_node_with_dag input_dag copy on condition""" qc = QuantumCircuit(2, 1) - qc.cx(0, 1).c_if(qc.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(qc.cregs[0], 1) qc.cx(1, 0) circ = transpile(qc, basis_gates=["u3", "cz"]) self.assertIsInstance(circ, QuantumCircuit) diff --git a/test/python/transpiler/test_remove_diagonal_gates_before_measure.py b/test/python/transpiler/test_remove_diagonal_gates_before_measure.py index 474a586448b8..f560aa2c4855 100644 --- a/test/python/transpiler/test_remove_diagonal_gates_before_measure.py +++ b/test/python/transpiler/test_remove_diagonal_gates_before_measure.py @@ -550,7 +550,8 @@ def test_do_not_optimize_with_conditional(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.rz(0.1, qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.rz(0.1, qr[1]).c_if(cr, 1) circuit.barrier() circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) diff --git a/test/python/transpiler/test_remove_final_measurements.py b/test/python/transpiler/test_remove_final_measurements.py index 4bc0e107109b..a91687bd8f3e 100644 --- a/test/python/transpiler/test_remove_final_measurements.py +++ b/test/python/transpiler/test_remove_final_measurements.py @@ -57,7 +57,8 @@ def expected_dag(): q0 = QuantumRegister(1, "q0") c0 = ClassicalRegister(1, "c0") qc = QuantumCircuit(q0, c0) - qc.x(0).c_if(c0[0], 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(c0[0], 0) return circuit_to_dag(qc) q0 = QuantumRegister(1, "q0") @@ -65,7 +66,8 @@ def expected_dag(): qc = QuantumCircuit(q0, c0) # make c0 busy - qc.x(0).c_if(c0[0], 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(c0[0], 0) # measure into c0 qc.measure(0, c0[0]) @@ -87,7 +89,8 @@ def expected_dag(): q0 = QuantumRegister(1, "q0") c0 = ClassicalRegister(2, "c0") qc = QuantumCircuit(q0, c0) - qc.x(q0[0]).c_if(c0[0], 0) + with self.assertWarns(DeprecationWarning): + qc.x(q0[0]).c_if(c0[0], 0) return circuit_to_dag(qc) q0 = QuantumRegister(1, "q0") @@ -95,7 +98,8 @@ def expected_dag(): qc = QuantumCircuit(q0, c0) # make c0[0] busy - qc.x(q0[0]).c_if(c0[0], 0) + with self.assertWarns(DeprecationWarning): + qc.x(q0[0]).c_if(c0[0], 0) # measure into not busy c0[1] qc.measure(0, c0[1]) diff --git a/test/python/transpiler/test_reset_after_measure_simplification.py b/test/python/transpiler/test_reset_after_measure_simplification.py index 38f602443c06..976801e71353 100644 --- a/test/python/transpiler/test_reset_after_measure_simplification.py +++ b/test/python/transpiler/test_reset_after_measure_simplification.py @@ -26,12 +26,13 @@ def test_simple(self): qc = QuantumCircuit(1, 1) qc.measure(0, 0) qc.reset(0) - - new_qc = ResetAfterMeasureSimplification()(qc) + with self.assertWarns(DeprecationWarning): + new_qc = ResetAfterMeasureSimplification()(qc) ans_qc = QuantumCircuit(1, 1) ans_qc.measure(0, 0) - ans_qc.x(0).c_if(ans_qc.clbits[0], 1) + with self.assertWarns(DeprecationWarning): + ans_qc.x(0).c_if(ans_qc.clbits[0], 1) self.assertEqual(new_qc, ans_qc) def test_simple_null(self): @@ -52,12 +53,13 @@ def test_simple_multi_reg(self): qc = QuantumCircuit(qr, cr1, cr2) qc.measure(0, 1) qc.reset(0) - - new_qc = ResetAfterMeasureSimplification()(qc) + with self.assertWarns(DeprecationWarning): + new_qc = ResetAfterMeasureSimplification()(qc) ans_qc = QuantumCircuit(qr, cr1, cr2) ans_qc.measure(0, 1) - ans_qc.x(0).c_if(cr2[0], 1) + with self.assertWarns(DeprecationWarning): + ans_qc.x(0).c_if(cr2[0], 1) self.assertEqual(new_qc, ans_qc) @@ -69,7 +71,6 @@ def test_simple_multi_reg_null(self): qc = QuantumCircuit(qr, cr1, cr2) qc.measure(0, 1) qc.reset(1) # reset not on same qubit as meas - new_qc = ResetAfterMeasureSimplification()(qc) self.assertEqual(new_qc, qc) @@ -79,12 +80,13 @@ def test_simple_multi_resets(self): qc.measure(0, 0) qc.reset(0) qc.reset(0) - - new_qc = ResetAfterMeasureSimplification()(qc) + with self.assertWarns(DeprecationWarning): + new_qc = ResetAfterMeasureSimplification()(qc) ans_qc = QuantumCircuit(1, 2) ans_qc.measure(0, 0) - ans_qc.x(0).c_if(ans_qc.clbits[0], 1) + with self.assertWarns(DeprecationWarning): + ans_qc.x(0).c_if(ans_qc.clbits[0], 1) ans_qc.reset(0) self.assertEqual(new_qc, ans_qc) @@ -96,11 +98,13 @@ def test_simple_multi_resets_with_resets_before_measure(self): qc.reset(1) qc.measure(1, 1) - new_qc = ResetAfterMeasureSimplification()(qc) + with self.assertWarns(DeprecationWarning): + new_qc = ResetAfterMeasureSimplification()(qc) ans_qc = QuantumCircuit(2, 2) ans_qc.measure(0, 0) - ans_qc.x(0).c_if(Clbit(ClassicalRegister(2, "c"), 0), 1) + with self.assertWarns(DeprecationWarning): + ans_qc.x(0).c_if(Clbit(ClassicalRegister(2, "c"), 0), 1) ans_qc.reset(1) ans_qc.measure(1, 1) @@ -134,7 +138,8 @@ def test_bv_circuit(self): qc.reset(1) qc.x(1) qc.h(1) - new_qc = ResetAfterMeasureSimplification()(qc) + with self.assertWarns(DeprecationWarning): + new_qc = ResetAfterMeasureSimplification()(qc) for op in new_qc.data: if op.operation.name == "reset": self.assertEqual(op.qubits[0], new_qc.qubits[1]) @@ -149,7 +154,8 @@ def test_simple_if_else(self): base_expected = QuantumCircuit(1, 1) base_expected.measure(0, 0) - base_expected.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + base_expected.x(0).c_if(0, True) test = QuantumCircuit(1, 1) test.if_else( @@ -165,7 +171,8 @@ def test_simple_if_else(self): expected.clbits, ) - self.assertEqual(pass_(test), expected) + with self.assertWarns(DeprecationWarning): + self.assertEqual(pass_(test), expected) def test_nested_control_flow(self): """Test that the pass recurses into nested control flow.""" @@ -177,7 +184,8 @@ def test_nested_control_flow(self): base_expected = QuantumCircuit(1, 1) base_expected.measure(0, 0) - base_expected.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + base_expected.x(0).c_if(0, True) body_test = QuantumCircuit(1, 1) body_test.for_loop((0,), None, base_expected.copy(), body_test.qubits, body_test.clbits) @@ -194,5 +202,5 @@ def test_nested_control_flow(self): expected.while_loop( (expected.clbits[0], True), body_expected, expected.qubits, expected.clbits ) - - self.assertEqual(pass_(test), expected) + with self.assertWarns(DeprecationWarning): + self.assertEqual(pass_(test), expected) diff --git a/test/python/transpiler/test_sabre_swap.py b/test/python/transpiler/test_sabre_swap.py index 856b5ff09f5b..e66f56c7cd34 100644 --- a/test/python/transpiler/test_sabre_swap.py +++ b/test/python/transpiler/test_sabre_swap.py @@ -293,7 +293,8 @@ def test_classical_condition(self): with self.subTest("1 bit in register"): qc = QuantumCircuit(2, 1) qc.z(0) - qc.z(0).c_if(qc.cregs[0], 0) + with self.assertWarns(DeprecationWarning): + qc.z(0).c_if(qc.cregs[0], 0) cm = CouplingMap([(0, 1), (1, 0)]) expected = PassManager([TrivialLayout(cm)]).run(qc) actual = PassManager([TrivialLayout(cm), SabreSwap(cm)]).run(qc) @@ -302,8 +303,10 @@ def test_classical_condition(self): cregs = [ClassicalRegister(3), ClassicalRegister(4)] qc = QuantumCircuit(QuantumRegister(2, name="q"), *cregs) qc.z(0) - qc.z(0).c_if(cregs[0], 0) - qc.z(0).c_if(cregs[1], 0) + with self.assertWarns(DeprecationWarning): + qc.z(0).c_if(cregs[0], 0) + with self.assertWarns(DeprecationWarning): + qc.z(0).c_if(cregs[1], 0) cm = CouplingMap([(0, 1), (1, 0)]) expected = PassManager([TrivialLayout(cm)]).run(qc) actual = PassManager([TrivialLayout(cm), SabreSwap(cm)]).run(qc) @@ -316,40 +319,54 @@ def test_classical_condition_cargs(self): """ with self.subTest("missing measurement"): qc = QuantumCircuit(3, 1) - qc.cx(0, 2).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 2).c_if(0, 0) qc.measure(1, 0) - qc.h(2).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.h(2).c_if(0, 0) expected = QuantumCircuit(3, 1) expected.swap(1, 2) - expected.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected.cx(0, 1).c_if(0, 0) expected.measure(2, 0) - expected.h(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected.h(1).c_if(0, 0) result = SabreSwap(CouplingMap.from_line(3), seed=12345)(qc) self.assertEqual(result, expected) with self.subTest("reordered measurement"): qc = QuantumCircuit(3, 1) - qc.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(0, 0) qc.measure(1, 0) - qc.h(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.h(0).c_if(0, 0) expected = QuantumCircuit(3, 1) - expected.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected.cx(0, 1).c_if(0, 0) expected.measure(1, 0) - expected.h(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected.h(0).c_if(0, 0) result = SabreSwap(CouplingMap.from_line(3), seed=12345)(qc) self.assertEqual(result, expected) def test_conditional_measurement(self): """Test that instructions with cargs and conditions are handled correctly.""" qc = QuantumCircuit(3, 2) - qc.cx(0, 2).c_if(0, 0) - qc.measure(2, 0).c_if(1, 0) - qc.h(2).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 2).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.measure(2, 0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + qc.h(2).c_if(0, 0) qc.measure(1, 1) expected = QuantumCircuit(3, 2) expected.swap(1, 2) - expected.cx(0, 1).c_if(0, 0) - expected.measure(1, 0).c_if(1, 0) - expected.h(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected.measure(1, 0).c_if(1, 0) + with self.assertWarns(DeprecationWarning): + expected.h(1).c_if(0, 0) expected.measure(2, 1) result = SabreSwap(CouplingMap.from_line(3), seed=12345)(qc) self.assertEqual(result, expected) diff --git a/test/python/transpiler/test_scheduling_padding_pass.py b/test/python/transpiler/test_scheduling_padding_pass.py index f2f4f6a24bc6..32431ba6b2a2 100644 --- a/test/python/transpiler/test_scheduling_padding_pass.py +++ b/test/python/transpiler/test_scheduling_padding_pass.py @@ -114,7 +114,8 @@ def test_classically_controlled_gate_after_measure(self, schedule_pass): """ qc = QuantumCircuit(2, 1) qc.measure(0, 0) - qc.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, True) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) pm = PassManager([schedule_pass(durations), PadDelay()]) @@ -123,7 +124,8 @@ def test_classically_controlled_gate_after_measure(self, schedule_pass): expected = QuantumCircuit(2, 1) expected.measure(0, 0) expected.delay(1000, 1) # x.c_if starts after measure - expected.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, True) expected.delay(200, 0) self.assertEqual(expected, scheduled) @@ -200,8 +202,10 @@ def test_c_if_on_different_qubits(self, schedule_pass): """ qc = QuantumCircuit(3, 1) qc.measure(0, 0) - qc.x(1).c_if(0, True) - qc.x(2).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(2).c_if(0, True) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) pm = PassManager([schedule_pass(durations), PadDelay()]) @@ -211,8 +215,10 @@ def test_c_if_on_different_qubits(self, schedule_pass): expected.measure(0, 0) expected.delay(1000, 1) expected.delay(1000, 2) - expected.x(1).c_if(0, True) - expected.x(2).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(2).c_if(0, True) expected.delay(200, 0) self.assertEqual(expected, scheduled) @@ -283,7 +289,8 @@ def test_measure_after_c_if(self, schedule_pass): """ qc = QuantumCircuit(3, 1) qc.measure(0, 0) - qc.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 1) qc.measure(2, 0) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) @@ -294,7 +301,8 @@ def test_measure_after_c_if(self, schedule_pass): expected.delay(1000, 1) expected.delay(1000, 2) expected.measure(0, 0) - expected.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, 1) expected.measure(2, 0) expected.delay(1000, 0) expected.delay(800, 1) @@ -474,7 +482,8 @@ def test_measure_after_c_if_on_edge_locking(self): """ qc = QuantumCircuit(3, 1) qc.measure(0, 0) - qc.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 1) qc.measure(2, 0) durations = InstructionDurations([("x", None, 200), ("measure", None, 1000)]) @@ -499,7 +508,8 @@ def test_measure_after_c_if_on_edge_locking(self): expected_asap = QuantumCircuit(3, 1) expected_asap.measure(0, 0) expected_asap.delay(1000, 1) - expected_asap.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_asap.x(1).c_if(0, 1) expected_asap.measure(2, 0) expected_asap.delay(200, 0) expected_asap.delay(200, 2) @@ -508,7 +518,8 @@ def test_measure_after_c_if_on_edge_locking(self): expected_alap = QuantumCircuit(3, 1) expected_alap.measure(0, 0) expected_alap.delay(1000, 1) - expected_alap.x(1).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_alap.x(1).c_if(0, 1) expected_alap.delay(200, 2) expected_alap.measure(2, 0) expected_alap.delay(200, 0) @@ -534,11 +545,14 @@ def test_active_reset_circuit(self, write_lat, cond_lat): """ qc = QuantumCircuit(1, 1) qc.measure(0, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) qc.measure(0, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) qc.measure(0, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) durations = InstructionDurations([("x", None, 100), ("measure", None, 1000)]) @@ -562,15 +576,18 @@ def test_active_reset_circuit(self, write_lat, cond_lat): expected.measure(0, 0) if cond_lat > 0: expected.delay(cond_lat, 0) - expected.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, 1) expected.measure(0, 0) if cond_lat > 0: expected.delay(cond_lat, 0) - expected.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, 1) expected.measure(0, 0) if cond_lat > 0: expected.delay(cond_lat, 0) - expected.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, 1) self.assertEqual(expected, actual_asap) self.assertEqual(expected, actual_alap) @@ -659,15 +676,19 @@ def test_random_complicated_circuit(self): """ qc = QuantumCircuit(3, 1) qc.delay(100, 0) - qc.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 1) qc.barrier() qc.measure(2, 0) - qc.x(1).c_if(0, 0) - qc.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, 0) qc.delay(300, 0) qc.cx(1, 2) qc.x(0) - qc.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + qc.cx(0, 1).c_if(0, 0) qc.measure(2, 0) durations = InstructionDurations( @@ -694,19 +715,23 @@ def test_random_complicated_circuit(self): expected_asap.delay(200, 0) # due to conditional latency of 200dt expected_asap.delay(300, 1) expected_asap.delay(300, 2) - expected_asap.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_asap.x(0).c_if(0, 1) expected_asap.barrier() expected_asap.delay(1400, 0) expected_asap.delay(1200, 1) expected_asap.measure(2, 0) - expected_asap.x(1).c_if(0, 0) - expected_asap.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_asap.x(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_asap.x(0).c_if(0, 0) expected_asap.delay(300, 0) expected_asap.x(0) expected_asap.delay(300, 2) expected_asap.cx(1, 2) expected_asap.delay(400, 1) - expected_asap.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_asap.cx(0, 1).c_if(0, 0) expected_asap.delay(700, 0) # creg is released at t0 of cx(0,1).c_if(0,0) expected_asap.delay( 700, 1 @@ -720,20 +745,24 @@ def test_random_complicated_circuit(self): expected_alap.delay(200, 0) # due to conditional latency of 200dt expected_alap.delay(300, 1) expected_alap.delay(300, 2) - expected_alap.x(0).c_if(0, 1) + with self.assertWarns(DeprecationWarning): + expected_alap.x(0).c_if(0, 1) expected_alap.barrier() expected_alap.delay(1400, 0) expected_alap.delay(1200, 1) expected_alap.measure(2, 0) - expected_alap.x(1).c_if(0, 0) - expected_alap.x(0).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_alap.x(1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_alap.x(0).c_if(0, 0) expected_alap.delay(300, 0) expected_alap.x(0) expected_alap.delay(300, 1) expected_alap.delay(600, 2) expected_alap.cx(1, 2) expected_alap.delay(100, 1) - expected_alap.cx(0, 1).c_if(0, 0) + with self.assertWarns(DeprecationWarning): + expected_alap.cx(0, 1).c_if(0, 0) expected_alap.measure(2, 0) expected_alap.delay(700, 0) expected_alap.delay(700, 1) @@ -771,8 +800,10 @@ def test_dag_introduces_extra_dependency_between_conditionals(self): """ qc = QuantumCircuit(2, 1) qc.delay(100, 0) - qc.x(0).c_if(0, True) - qc.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.x(1).c_if(0, True) durations = InstructionDurations([("x", None, 160)]) pm = PassManager([ASAPScheduleAnalysis(durations), PadDelay()]) @@ -781,8 +812,10 @@ def test_dag_introduces_extra_dependency_between_conditionals(self): expected = QuantumCircuit(2, 1) expected.delay(100, 0) expected.delay(100, 1) # due to extra dependency on clbits - expected.x(0).c_if(0, True) - expected.x(1).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(0).c_if(0, True) + with self.assertWarns(DeprecationWarning): + expected.x(1).c_if(0, True) self.assertEqual(expected, scheduled) diff --git a/test/python/transpiler/test_unroll_3q_or_more.py b/test/python/transpiler/test_unroll_3q_or_more.py index 27927bbbd4ad..d0388e4fe531 100644 --- a/test/python/transpiler/test_unroll_3q_or_more.py +++ b/test/python/transpiler/test_unroll_3q_or_more.py @@ -62,7 +62,8 @@ def test_decompose_conditional(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 0) + with self.assertWarns(DeprecationWarning): + circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 0) dag = circuit_to_dag(circuit) pass_ = Unroll3qOrMore() after_dag = pass_.run(dag) diff --git a/test/python/transpiler/test_unroll_forloops.py b/test/python/transpiler/test_unroll_forloops.py index cbf8f70ef767..a44655903370 100644 --- a/test/python/transpiler/test_unroll_forloops.py +++ b/test/python/transpiler/test_unroll_forloops.py @@ -135,7 +135,8 @@ def test_skip_continue_c_if(self): circuit.h(0) circuit.cx(0, 1) circuit.measure(0, 0) - circuit.break_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + circuit.break_loop().c_if(0, True) passmanager = PassManager() passmanager.append(UnrollForLoops()) diff --git a/test/python/visualization/test_circuit_drawer.py b/test/python/visualization/test_circuit_drawer.py index dd69faac02cb..9eb4e3ad2c88 100644 --- a/test/python/visualization/test_circuit_drawer.py +++ b/test/python/visualization/test_circuit_drawer.py @@ -151,7 +151,8 @@ def test_wire_order(self): circuit.h(0) circuit.h(3) circuit.x(1) - circuit.x(3).c_if(cr, 10) + with self.assertWarns(DeprecationWarning): + circuit.x(3).c_if(cr, 10) expected = "\n".join( [ @@ -183,7 +184,8 @@ def test_wire_order_cregbundle(self): circuit.h(0) circuit.h(3) circuit.x(1) - circuit.x(3).c_if(cr, 10) + with self.assertWarns(DeprecationWarning): + circuit.x(3).c_if(cr, 10) expected = "\n".join( [ diff --git a/test/python/visualization/test_circuit_text_drawer.py b/test/python/visualization/test_circuit_text_drawer.py index e7d28aac8a9e..666255c44f23 100644 --- a/test/python/visualization/test_circuit_text_drawer.py +++ b/test/python/visualization/test_circuit_text_drawer.py @@ -388,7 +388,8 @@ def test_wire_order(self): circuit.h(0) circuit.h(3) circuit.x(1) - circuit.x(3).c_if(cr, 10) + with self.assertWarns(DeprecationWarning): + circuit.x(3).c_if(cr, 10) self.assertEqual( str( circuit_drawer( @@ -845,7 +846,8 @@ def test_text_cu1_condition(self): qr = QuantumRegister(3, "q") cr = ClassicalRegister(3, "c") circuit = QuantumCircuit(qr, cr) - circuit.append(CU1Gate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CU1Gate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) self.assertEqual(str(circuit_drawer(circuit, output="text", initial_state=False)), expected) def test_text_rzz_condition(self): @@ -866,7 +868,8 @@ def test_text_rzz_condition(self): qr = QuantumRegister(3, "q") cr = ClassicalRegister(3, "c") circuit = QuantumCircuit(qr, cr) - circuit.append(RZZGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) + with self.assertWarns(DeprecationWarning): + circuit.append(RZZGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) self.assertEqual(str(circuit_drawer(circuit, output="text", initial_state=False)), expected) def test_text_cp_condition(self): @@ -887,7 +890,8 @@ def test_text_cp_condition(self): qr = QuantumRegister(3, "q") cr = ClassicalRegister(3, "c") circuit = QuantumCircuit(qr, cr) - circuit.append(CPhaseGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CPhaseGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) self.assertEqual(str(circuit_drawer(circuit, output="text", initial_state=False)), expected) def test_text_cu1_reverse_bits(self): @@ -1758,7 +1762,8 @@ def test_control_gate_label_with_cond_1_low(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [0, 1]) self.assertEqual( @@ -1789,7 +1794,8 @@ def test_control_gate_label_with_cond_1_low_cregbundle(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [0, 1]) self.assertEqual( @@ -1824,7 +1830,8 @@ def test_control_gate_label_with_cond_1_med(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [0, 1]) self.assertEqual( @@ -1860,7 +1867,8 @@ def test_control_gate_label_with_cond_1_med_cregbundle(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [0, 1]) self.assertEqual( @@ -1895,7 +1903,8 @@ def test_control_gate_label_with_cond_1_high(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [0, 1]) self.assertEqual( @@ -1930,7 +1939,8 @@ def test_control_gate_label_with_cond_1_high_cregbundle(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [0, 1]) self.assertEqual( @@ -1966,7 +1976,8 @@ def test_control_gate_label_with_cond_2_med_space(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [1, 0]) self.assertEqual( @@ -1998,7 +2009,8 @@ def test_control_gate_label_with_cond_2_med(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ctrl-h").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ctrl-h").c_if(cr, 1) circ.append(controlh, [1, 0]) self.assertEqual( @@ -2034,7 +2046,8 @@ def test_control_gate_label_with_cond_2_med_cregbundle(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [1, 0]) self.assertEqual( @@ -2071,7 +2084,8 @@ def test_control_gate_label_with_cond_2_low(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [1, 0]) self.assertEqual( @@ -2108,7 +2122,8 @@ def test_control_gate_label_with_cond_2_low_cregbundle(self): cr = ClassicalRegister(1, "c") circ = QuantumCircuit(qr, cr) hgate = HGate(label="my h") - controlh = hgate.control(label="my ch").c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + controlh = hgate.control(label="my ch").c_if(cr, 1) circ.append(controlh, [1, 0]) self.assertEqual( @@ -2269,8 +2284,8 @@ def test_text_conditional_1(self): " 0x1 ", ] ) - - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2309,7 +2324,8 @@ def test_text_conditional_1_bundle(self): ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2335,7 +2351,8 @@ def test_text_conditional_reverse_bits_true(self): circuit.x(0) circuit.x(0) circuit.measure(2, 1) - circuit.x(2).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.x(2).c_if(cr, 2) expected = "\n".join( [ @@ -2386,7 +2403,8 @@ def test_text_conditional_reverse_bits_false(self): circuit.x(0) circuit.x(0) circuit.measure(2, 1) - circuit.x(2).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.x(2).c_if(cr, 2) expected = "\n".join( [ @@ -2486,7 +2504,8 @@ def test_text_conditional_1(self): " 0x1 ", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2523,8 +2542,8 @@ def test_text_conditional_1_bundle(self): " └─────┘", ] ) - - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2565,7 +2584,8 @@ def test_text_measure_with_spaces(self): " 0x1 ", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2603,7 +2623,8 @@ def test_text_measure_with_spaces_bundle(self): " 1 └─────┘", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2694,8 +2715,10 @@ def test_text_barrier_med_compress_3(self): qc1 = ClassicalRegister(3, "cr") qc2 = ClassicalRegister(1, "cr2") circuit = QuantumCircuit(qr, qc1, qc2) - circuit.x(0).c_if(qc1, 3) - circuit.x(0).c_if(qc2[0], 1) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(qc1, 3) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(qc2[0], 1) expected = "\n".join( [ @@ -2753,8 +2776,8 @@ def test_text_conditional_1_cregbundle(self): " └─────┘", ] ) - - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2790,8 +2813,8 @@ def test_text_conditional_1(self): " 0x1 ", ] ) - - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str(circuit_drawer(circuit, output="text", initial_state=True, cregbundle=False)), expected, @@ -2819,7 +2842,8 @@ def test_text_conditional_2_cregbundle(self): " └─────┘", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2859,7 +2883,8 @@ def test_text_conditional_2(self): " 0x2 ", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str(circuit_drawer(circuit, output="text", initial_state=True, cregbundle=False)), expected, @@ -2887,7 +2912,8 @@ def test_text_conditional_3_cregbundle(self): " └─────┘", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -2931,7 +2957,8 @@ def test_text_conditional_3(self): " 0x3 ", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str(circuit_drawer(circuit, output="text", initial_state=True, cregbundle=False)), expected, @@ -2959,7 +2986,8 @@ def test_text_conditional_4(self): " └─────┘", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str( circuit_drawer( @@ -3007,7 +3035,8 @@ def test_text_conditional_5(self): " 0x5 ", ] ) - circuit = QuantumCircuit.from_qasm_str(qasm_string) + with self.assertWarns(DeprecationWarning): + circuit = QuantumCircuit.from_qasm_str(qasm_string) self.assertEqual( str(circuit_drawer(circuit, output="text", initial_state=True, cregbundle=False)), expected, @@ -3018,7 +3047,8 @@ def test_text_conditional_cz_no_space_cregbundle(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cz(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cz(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3042,7 +3072,8 @@ def test_text_conditional_cz_no_space(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cz(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cz(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3066,7 +3097,8 @@ def test_text_conditional_cz_cregbundle(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cz(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cz(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3092,7 +3124,8 @@ def test_text_conditional_cz(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cz(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cz(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3118,7 +3151,8 @@ def test_text_conditional_cx_ct_cregbundle(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cx(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cx(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3144,7 +3178,8 @@ def test_text_conditional_cx_ct(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cx(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cx(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3170,7 +3205,8 @@ def test_text_conditional_cx_tc_cregbundle(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cx(qr[1], qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cx(qr[1], qr[0]).c_if(cr, 1) expected = "\n".join( [ @@ -3196,7 +3232,8 @@ def test_text_conditional_cx_tc(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cx(qr[1], qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cx(qr[1], qr[0]).c_if(cr, 1) expected = "\n".join( [ @@ -3222,7 +3259,8 @@ def test_text_conditional_cu3_ct_cregbundle(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[0], qr[1]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[0], qr[1]]).c_if(cr, 1) expected = "\n".join( [ @@ -3248,7 +3286,8 @@ def test_text_conditional_cu3_ct(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[0], qr[1]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[0], qr[1]]).c_if(cr, 1) expected = "\n".join( [ @@ -3274,7 +3313,8 @@ def test_text_conditional_cu3_tc_cregbundle(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[1], qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[1], qr[0]]).c_if(cr, 1) expected = "\n".join( [ @@ -3300,7 +3340,8 @@ def test_text_conditional_cu3_tc(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[1], qr[0]]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CU3Gate(pi / 2, pi / 2, pi / 2), [qr[1], qr[0]]).c_if(cr, 1) expected = "\n".join( [ @@ -3326,7 +3367,8 @@ def test_text_conditional_ccx_cregbundle(self): qr = QuantumRegister(4, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) expected = "\n".join( [ @@ -3354,7 +3396,8 @@ def test_text_conditional_ccx(self): qr = QuantumRegister(4, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) expected = "\n".join( [ @@ -3382,7 +3425,8 @@ def test_text_conditional_ccx_no_space_cregbundle(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) expected = "\n".join( [ @@ -3416,7 +3460,8 @@ def test_text_conditional_ccx_no_space(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.ccx(qr[0], qr[1], qr[2]).c_if(cr, 1) expected = "\n".join( [ @@ -3442,7 +3487,8 @@ def test_text_conditional_h_cregbundle(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr, 1) expected = "\n".join( [ @@ -3466,7 +3512,8 @@ def test_text_conditional_h(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr, 1) expected = "\n".join( [ @@ -3490,7 +3537,8 @@ def test_text_conditional_swap_cregbundle(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.swap(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.swap(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3516,7 +3564,8 @@ def test_text_conditional_swap(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.swap(qr[0], qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.swap(qr[0], qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3542,7 +3591,8 @@ def test_text_conditional_cswap_cregbundle(self): qr = QuantumRegister(4, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cswap(qr[0], qr[1], qr[2]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cswap(qr[0], qr[1], qr[2]).c_if(cr, 1) expected = "\n".join( [ @@ -3570,7 +3620,8 @@ def test_text_conditional_cswap(self): qr = QuantumRegister(4, "qr") cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.cswap(qr[0], qr[1], qr[2]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.cswap(qr[0], qr[1], qr[2]).c_if(cr, 1) expected = "\n".join( [ @@ -3599,7 +3650,8 @@ def test_conditional_reset_cregbundle(self): cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.reset(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.reset(qr[0]).c_if(cr, 1) expected = "\n".join( [ @@ -3624,7 +3676,8 @@ def test_conditional_reset(self): cr = ClassicalRegister(1, "cr") circuit = QuantumCircuit(qr, cr) - circuit.reset(qr[0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.reset(qr[0]).c_if(cr, 1) expected = "\n".join( [ @@ -3649,7 +3702,8 @@ def test_conditional_multiplexer_cregbundle(self): qr = QuantumRegister(3, name="qr") cr = ClassicalRegister(1, "cr") qc = QuantumCircuit(qr, cr) - qc.append(cx_multiplexer.c_if(cr, 1), [qr[0], qr[1]]) + with self.assertWarns(DeprecationWarning): + qc.append(cx_multiplexer.c_if(cr, 1), [qr[0], qr[1]]) expected = "\n".join( [ @@ -3675,7 +3729,8 @@ def test_conditional_multiplexer(self): qr = QuantumRegister(3, name="qr") cr = ClassicalRegister(1, "cr") qc = QuantumCircuit(qr, cr) - qc.append(cx_multiplexer.c_if(cr, 1), [qr[0], qr[1]]) + with self.assertWarns(DeprecationWarning): + qc.append(cx_multiplexer.c_if(cr, 1), [qr[0], qr[1]]) expected = "\n".join( [ @@ -3702,7 +3757,8 @@ def test_text_conditional_measure_cregbundle(self): circuit = QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) - circuit.h(qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3736,7 +3792,8 @@ def test_text_conditional_measure(self): circuit = QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) - circuit.h(qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3762,8 +3819,10 @@ def test_text_bit_conditional(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(2, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr[0], 1) - circuit.h(qr[1]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr[1], 0) expected = "\n".join( [ @@ -3790,8 +3849,10 @@ def test_text_bit_conditional_cregbundle(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(2, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr[0], 1) - circuit.h(qr[1]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr[1], 0) expected = "\n".join( [ @@ -3826,7 +3887,8 @@ def test_text_condition_measure_bits_true(self): cr = ClassicalRegister(2, "cr") crx = ClassicalRegister(3, "cs") circuit = QuantumCircuit(bits, cr, [Clbit()], crx) - circuit.x(0).c_if(crx[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(crx[1], 0) circuit.measure(0, bits[3]) expected = "\n".join( @@ -3860,7 +3922,8 @@ def test_text_condition_measure_bits_false(self): cr = ClassicalRegister(2, "cr") crx = ClassicalRegister(3, "cs") circuit = QuantumCircuit(bits, cr, [Clbit()], crx) - circuit.x(0).c_if(crx[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(crx[1], 0) circuit.measure(0, bits[3]) expected = "\n".join( @@ -3900,7 +3963,8 @@ def test_text_conditional_reverse_bits_1(self): circuit = QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) - circuit.h(qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr, 1) expected = "\n".join( [ @@ -3930,10 +3994,14 @@ def test_text_conditional_reverse_bits_2(self): qr = QuantumRegister(3, "qr") cr = ClassicalRegister(3, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr, 6) - circuit.h(qr[1]).c_if(cr, 1) - circuit.h(qr[2]).c_if(cr, 2) - circuit.cx(0, 1).c_if(cr, 3) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr, 6) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[2]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.cx(0, 1).c_if(cr, 3) expected = "\n".join( [ @@ -3969,7 +4037,8 @@ def test_text_condition_bits_reverse(self): cr = ClassicalRegister(2, "cr") crx = ClassicalRegister(3, "cs") circuit = QuantumCircuit(bits, cr, [Clbit()], crx) - circuit.x(0).c_if(bits[3], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(bits[3], 0) expected = "\n".join( [ @@ -4847,9 +4916,10 @@ def test_cccz_conditional(self): qr = QuantumRegister(4, "q") cr = ClassicalRegister(1, "c") circuit = QuantumCircuit(qr, cr) - circuit.append( - ZGate().control(3, ctrl_state="101").c_if(cr, 1), [qr[0], qr[1], qr[2], qr[3]] - ) + with self.assertWarns(DeprecationWarning): + circuit.append( + ZGate().control(3, ctrl_state="101").c_if(cr, 1), [qr[0], qr[1], qr[2], qr[3]] + ) self.assertEqual(str(circuit_drawer(circuit, output="text", initial_state=True)), expected) def test_cch_bot(self): @@ -5908,14 +5978,16 @@ def test_if_else_with_body_specified(self): circuit.measure(0, 1) circuit.measure(1, 2) circuit.x(2) - circuit.x(2, label="XLabel").c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.x(2, label="XLabel").c_if(cr, 2) qr2 = QuantumRegister(3, "qr2") circuit2 = QuantumCircuit(qr2, cr) circuit2.x(1) circuit2.y(1) circuit2.z(0) - circuit2.x(0, label="X1i").c_if(cr, 4) + with self.assertWarns(DeprecationWarning): + circuit2.x(0, label="X1i").c_if(cr, 4) circuit.if_else((cr[1], 1), circuit2, None, [0, 1, 2], [0, 1, 2]) circuit.x(0, label="X1i") @@ -5981,7 +6053,8 @@ def test_if_op_nested_wire_order(self): circuit.h(0) with circuit.if_test((cr[1], 1)) as _else: - circuit.x(0, label="X c_if").c_if(cr, 4) + with self.assertWarns(DeprecationWarning): + circuit.x(0, label="X c_if").c_if(cr, 4) with circuit.if_test((cr[2], 1)): circuit.z(0) circuit.y(1) @@ -6227,12 +6300,15 @@ def test_if_else_op_from_circuit_with_conditions(self): cr = ClassicalRegister(3, "cr") circuit = QuantumCircuit(qr, cr) circuit.h(0) - circuit.x(2).c_if(cr[1], 2) + with self.assertWarns(DeprecationWarning): + circuit.x(2).c_if(cr[1], 2) qr2 = QuantumRegister(3, "qr2") qc2 = QuantumCircuit(qr2, cr) - qc2.x(0, label="X1").c_if(cr, 4) - qc2.x(1, label="X2").c_if(cr[1], 1) + with self.assertWarns(DeprecationWarning): + qc2.x(0, label="X1").c_if(cr, 4) + with self.assertWarns(DeprecationWarning): + qc2.x(1, label="X2").c_if(cr[1], 1) circuit.if_else((cr[1], 1), qc2, None, [0, 1, 2], [0, 1, 2]) self.assertEqual( diff --git a/test/python/visualization/test_dag_drawer.py b/test/python/visualization/test_dag_drawer.py index 56138c12cf00..9c9b11e42a68 100644 --- a/test/python/visualization/test_dag_drawer.py +++ b/test/python/visualization/test_dag_drawer.py @@ -98,7 +98,8 @@ def test_dag_drawer_with_dag_dep(self): qc.cx(0, 1) qc.cx(0, 2) qc.cx(0, 3) - qc.x(3).c_if(cr[1], 1) + with self.assertWarns(DeprecationWarning): + qc.x(3).c_if(cr[1], 1) qc.h(3) qc.x(4) qc.barrier(0, 1) diff --git a/test/python/visualization/test_utils.py b/test/python/visualization/test_utils.py index 1ef87994d001..7e0f91f3f993 100644 --- a/test/python/visualization/test_utils.py +++ b/test/python/visualization/test_utils.py @@ -328,9 +328,13 @@ def test_get_layered_instructions_op_with_cargs(self): qc.h(0) qc.measure(0, 0) qc_2 = QuantumCircuit(1, 1, name="add_circ") - qc_2.h(0).c_if(qc_2.cregs[0], 1) + with self.assertWarns(DeprecationWarning): + qc_2.h(0).c_if(qc_2.cregs[0], 1) qc_2.measure(0, 0) - qc.append(qc_2, [1], [0]) + # This append calls ends up calling .to_instruction() which calls + # .c_if() internally + with self.assertWarns(DeprecationWarning): + qc.append(qc_2, [1], [0]) (_, _, layered_ops) = _utils._get_layered_instructions(qc) diff --git a/test/utils/base.py b/test/utils/base.py index 0038f7772ebf..cbfae18ac95e 100644 --- a/test/utils/base.py +++ b/test/utils/base.py @@ -164,6 +164,13 @@ def setUpClass(cls): module="qiskit.providers.fake_provider.fake_backend", ) + # Remove with the condition attribute in 2.0: + warnings.filterwarnings( + "ignore", + category=DeprecationWarning, + message=r".*The property.*Instruction\.condition.*is deprecated.*", + ) + allow_DeprecationWarning_message = [ r"The property ``qiskit\.circuit\.bit\.Bit\.(register|index)`` is deprecated.*", ] From 36695042d81a79cb9094ede3806f167e4898e970 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 26 Sep 2024 07:10:33 -0400 Subject: [PATCH 2/9] Add missing assertWarns --- .../primitives/test_backend_estimator.py | 3 +- .../python/primitives/test_backend_sampler.py | 3 +- .../visualization/test_circuit_latex.py | 45 ++++++++++++------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/test/python/primitives/test_backend_estimator.py b/test/python/primitives/test_backend_estimator.py index 80b471b66063..62845461dacf 100644 --- a/test/python/primitives/test_backend_estimator.py +++ b/test/python/primitives/test_backend_estimator.py @@ -493,7 +493,8 @@ def test_dynamic_circuit(self): qc.h(0) qc.cx(0, 1) qc.measure(1, 0) - qc.break_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.break_loop().c_if(0, True) observable = SparsePauliOp("IZ") diff --git a/test/python/primitives/test_backend_sampler.py b/test/python/primitives/test_backend_sampler.py index 27e438d30874..9783ac7991ff 100644 --- a/test/python/primitives/test_backend_sampler.py +++ b/test/python/primitives/test_backend_sampler.py @@ -396,7 +396,8 @@ def test_circuit_with_dynamic_circuit(self): qc.h(0) qc.cx(0, 1) qc.measure(0, 0) - qc.break_loop().c_if(0, True) + with self.assertWarns(DeprecationWarning): + qc.break_loop().c_if(0, True) with self.assertWarns(DeprecationWarning): backend = Aer.get_backend("aer_simulator") diff --git a/test/python/visualization/test_circuit_latex.py b/test/python/visualization/test_circuit_latex.py index bcf5b77d51bd..8f33f1aa3ce8 100644 --- a/test/python/visualization/test_circuit_latex.py +++ b/test/python/visualization/test_circuit_latex.py @@ -99,7 +99,8 @@ def test_4597(self): qr = QuantumRegister(3, "q") cr = ClassicalRegister(3, "c") circuit = QuantumCircuit(qr, cr) - circuit.x(qr[2]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.x(qr[2]).c_if(cr, 2) circuit.draw(output="latex_source", cregbundle=True) circuit_drawer(circuit, filename=filename, output="latex_source") @@ -148,8 +149,10 @@ def test_teleport(self): circuit.measure(qr[0], cr[0]) circuit.measure(qr[1], cr[1]) # Apply a correction - circuit.z(qr[2]).c_if(cr, 1) - circuit.x(qr[2]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.z(qr[2]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.x(qr[2]).c_if(cr, 2) circuit.measure(qr[2], cr[2]) circuit_drawer(circuit, filename=filename, output="latex_source") @@ -206,7 +209,8 @@ def test_conditional(self): # check gates are shifted over accordingly circuit.h(qr) circuit.measure(qr, cr) - circuit.h(qr[0]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr, 2) circuit_drawer(circuit, filename=filename, output="latex_source") @@ -570,7 +574,8 @@ def test_meas_condition(self): circuit = QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) - circuit.h(qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr, 1) circuit_drawer(circuit, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -598,8 +603,10 @@ def test_cif_single_bit(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(2, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr[1], 0) - circuit.x(qr[1]).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(qr[1]).c_if(cr[0], 1) circuit_drawer(circuit, cregbundle=False, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -611,8 +618,10 @@ def test_cif_single_bit_cregbundle(self): qr = QuantumRegister(2, "qr") cr = ClassicalRegister(2, "cr") circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]).c_if(cr[1], 0) - circuit.x(qr[1]).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(qr[1]).c_if(cr[0], 1) circuit_drawer(circuit, cregbundle=True, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -639,8 +648,10 @@ def test_measures_with_conditions(self): circuit.h(0) circuit.h(1) circuit.measure(0, cr1[1]) - circuit.measure(1, cr2[0]).c_if(cr1, 1) - circuit.h(0).c_if(cr2, 3) + with self.assertWarns(DeprecationWarning): + circuit.measure(1, cr2[0]).c_if(cr1, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(0).c_if(cr2, 3) circuit_drawer(circuit, cregbundle=False, filename=filename1, output="latex_source") circuit_drawer(circuit, cregbundle=True, filename=filename2, output="latex_source") self.assertEqualToReference(filename1) @@ -654,7 +665,8 @@ def test_measures_with_conditions_with_bits(self): cr = ClassicalRegister(2, "cr") crx = ClassicalRegister(3, "cs") circuit = QuantumCircuit(bits, cr, [Clbit()], crx) - circuit.x(0).c_if(crx[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(crx[1], 0) circuit.measure(0, bits[3]) circuit_drawer(circuit, cregbundle=False, filename=filename1, output="latex_source") circuit_drawer(circuit, cregbundle=True, filename=filename2, output="latex_source") @@ -668,7 +680,8 @@ def test_conditions_with_bits_reverse(self): cr = ClassicalRegister(2, "cr") crx = ClassicalRegister(3, "cs") circuit = QuantumCircuit(bits, cr, [Clbit()], crx) - circuit.x(0).c_if(bits[3], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(bits[3], 0) circuit_drawer( circuit, cregbundle=False, reverse_bits=True, filename=filename, output="latex_source" ) @@ -680,7 +693,8 @@ def test_sidetext_with_condition(self): qr = QuantumRegister(2, "q") cr = ClassicalRegister(2, "c") circuit = QuantumCircuit(qr, cr) - circuit.append(CPhaseGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CPhaseGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) circuit_drawer(circuit, cregbundle=False, filename=filename, output="latex_source") self.assertEqualToReference(filename) @@ -703,7 +717,8 @@ def test_wire_order(self): circuit.h(0) circuit.h(3) circuit.x(1) - circuit.x(3).c_if(cr, 12) + with self.assertWarns(DeprecationWarning): + circuit.x(3).c_if(cr, 12) circuit_drawer( circuit, cregbundle=False, From ecd2976aa091a9bc3a5138a4349732f51197a18d Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 26 Sep 2024 07:58:18 -0400 Subject: [PATCH 3/9] Handle deprecation warnings in visual tests too --- .../circuit/test_circuit_matplotlib_drawer.py | 108 ++++++++++++------ 1 file changed, 72 insertions(+), 36 deletions(-) diff --git a/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py b/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py index 9e3dd5cc48eb..e10496a893c7 100644 --- a/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py +++ b/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py @@ -339,7 +339,8 @@ def test_conditional(self): # check gates are shifted over accordingly circuit.h(qr) circuit.measure(qr, cr) - circuit.h(qr[0]).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr, 2) fname = "reg_conditional.png" self.circuit_drawer(circuit, output="mpl", filename=fname) @@ -362,8 +363,10 @@ def test_bit_conditional_with_cregbundle(self): circuit.x(qr[0]) circuit.measure(qr, cr) - circuit.h(qr[0]).c_if(cr[0], 1) - circuit.x(qr[1]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + circuit.x(qr[1]).c_if(cr[1], 0) fname = "bit_conditional_bundle.png" self.circuit_drawer(circuit, output="mpl", filename=fname) @@ -386,8 +389,10 @@ def test_bit_conditional_no_cregbundle(self): circuit.x(qr[0]) circuit.measure(qr, cr) - circuit.h(qr[0]).c_if(cr[0], 1) - circuit.x(qr[1]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[0]).c_if(cr[0], 1) + with self.assertWarns(DeprecationWarning): + circuit.x(qr[1]).c_if(cr[1], 0) fname = "bit_conditional_no_bundle.png" self.circuit_drawer(circuit, output="mpl", filename=fname, cregbundle=False) @@ -1073,7 +1078,8 @@ def test_meas_condition(self): circuit = QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.measure(qr[0], cr[0]) - circuit.h(qr[1]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr, 1) fname = "meas_condition.png" self.circuit_drawer(circuit, output="mpl", filename=fname) @@ -1099,7 +1105,8 @@ def test_reverse_bits_condition(self): circuit.x(0) circuit.x(0) circuit.measure(2, 1) - circuit.x(2).c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.x(2).c_if(cr, 2) fname = "reverse_bits_cond_true.png" self.circuit_drawer( @@ -1394,8 +1401,10 @@ def test_measures_with_conditions(self): circuit.h(0) circuit.h(1) circuit.measure(0, cr1[1]) - circuit.measure(1, cr2[0]).c_if(cr1, 1) - circuit.h(0).c_if(cr2, 3) + with self.assertWarns(DeprecationWarning): + circuit.measure(1, cr2[0]).c_if(cr1, 1) + with self.assertWarns(DeprecationWarning): + circuit.h(0).c_if(cr2, 3) fname = "measure_cond_false.png" self.circuit_drawer(circuit, output="mpl", cregbundle=False, filename=fname) @@ -1427,7 +1436,8 @@ def test_conditions_measures_with_bits(self): cr = ClassicalRegister(2, "cr") crx = ClassicalRegister(3, "cs") circuit = QuantumCircuit(bits, cr, [Clbit()], crx) - circuit.x(0).c_if(crx[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(crx[1], 0) circuit.measure(0, bits[3]) fname = "measure_cond_bits_false.png" @@ -1461,8 +1471,10 @@ def test_conditional_gates_right_of_measures_with_bits(self): circuit = QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.measure(qr[0], cr[1]) - circuit.h(qr[1]).c_if(cr[1], 0) - circuit.h(qr[2]).c_if(cr[0], 0) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[1]).c_if(cr[1], 0) + with self.assertWarns(DeprecationWarning): + circuit.h(qr[2]).c_if(cr[0], 0) fname = "measure_cond_bits_right.png" self.circuit_drawer(circuit, output="mpl", cregbundle=False, filename=fname) @@ -1482,7 +1494,8 @@ def test_conditions_with_bits_reverse(self): cr = ClassicalRegister(2, "cr") crx = ClassicalRegister(2, "cs") circuit = QuantumCircuit(bits, cr, [Clbit()], crx) - circuit.x(0).c_if(bits[3], 0) + with self.assertWarns(DeprecationWarning): + circuit.x(0).c_if(bits[3], 0) fname = "cond_bits_reverse.png" self.circuit_drawer( @@ -1503,7 +1516,8 @@ def test_sidetext_with_condition(self): qr = QuantumRegister(2, "q") cr = ClassicalRegister(2, "c") circuit = QuantumCircuit(qr, cr) - circuit.append(CPhaseGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) + with self.assertWarns(DeprecationWarning): + circuit.append(CPhaseGate(pi / 2), [qr[0], qr[1]]).c_if(cr[1], 1) fname = "sidetext_condition.png" self.circuit_drawer(circuit, output="mpl", cregbundle=False, filename=fname) @@ -1523,22 +1537,38 @@ def test_fold_with_conditions(self): cr = ClassicalRegister(5, "cr") circuit = QuantumCircuit(qr, cr) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 1) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 3) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 5) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 7) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 9) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 11) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 13) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 15) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 17) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 19) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 21) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 23) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 25) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 27) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 29) - circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 31) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 1) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 3) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 5) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 7) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 9) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 11) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 13) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 15) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 17) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 19) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 21) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 23) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 25) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 27) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 29) + with self.assertWarns(DeprecationWarning): + circuit.append(U1Gate(0).control(1), [1, 0]).c_if(cr, 31) fname = "fold_with_conditions.png" self.circuit_drawer(circuit, output="mpl", cregbundle=False, filename=fname) @@ -1579,7 +1609,8 @@ def test_wire_order(self): circuit.h(0) circuit.h(3) circuit.x(1) - circuit.x(3).c_if(cr, 10) + with self.assertWarns(DeprecationWarning): + circuit.x(3).c_if(cr, 10) fname = "wire_order.png" self.circuit_drawer( @@ -1728,14 +1759,16 @@ def test_if_else_with_body(self): circuit.measure(0, 1) circuit.measure(1, 2) circuit.x(2) - circuit.x(2, label="XLabel").c_if(cr, 2) + with self.assertWarns(DeprecationWarning): + circuit.x(2, label="XLabel").c_if(cr, 2) qr2 = QuantumRegister(3, "qr2") qc2 = QuantumCircuit(qr2, cr) qc2.x(1) qc2.y(1) qc2.z(0) - qc2.x(0, label="X1i").c_if(cr, 4) + with self.assertWarns(DeprecationWarning): + qc2.x(0, label="X1i").c_if(cr, 4) circuit.if_else((cr[1], 1), qc2, None, [0, 1, 2], [0, 1, 2]) circuit.x(0, label="X1i") @@ -1760,7 +1793,8 @@ def test_if_else_op_nested(self): circuit.h(0) with circuit.if_test((cr[1], 1)) as _else: - circuit.x(0, label="X c_if").c_if(cr, 4) + with self.assertWarns(DeprecationWarning): + circuit.x(0, label="X c_if").c_if(cr, 4) with circuit.if_test((cr[2], 1)): circuit.z(0) circuit.y(1) @@ -1801,7 +1835,8 @@ def test_if_else_op_wire_order(self): circuit.h(0) with circuit.if_test((cr[1], 1)) as _else: - circuit.x(0, label="X c_if").c_if(cr, 4) + with self.assertWarns(DeprecationWarning): + circuit.x(0, label="X c_if").c_if(cr, 4) with circuit.if_test((cr[2], 1)): circuit.z(0) circuit.y(1) @@ -1848,7 +1883,8 @@ def test_if_else_op_fold(self): circuit.h(0) with circuit.if_test((cr[1], 1)) as _else: - circuit.x(0, label="X c_if").c_if(cr, 4) + with self.assertWarns(DeprecationWarning): + circuit.x(0, label="X c_if").c_if(cr, 4) with circuit.if_test((cr[2], 1)): circuit.z(0) circuit.y(1) From 576b52397641d71d961989003f25a586f59d57ea Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 26 Sep 2024 14:29:47 -0400 Subject: [PATCH 4/9] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com> --- qiskit/circuit/instruction.py | 2 +- .../deprecate-condition_c_if-9548e5522814fe9c.yaml | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py index 100a1f31a0a9..98f4c11b8233 100644 --- a/qiskit/circuit/instruction.py +++ b/qiskit/circuit/instruction.py @@ -160,7 +160,7 @@ def to_mutable(self): return self.copy() @property - @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0", is_property=True, stacklevel=2) + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0", is_property=True) def condition(self): """The classical condition on the instruction.""" return self._condition diff --git a/releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml b/releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml index 20c5ec38a022..e096273f3c8e 100644 --- a/releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml +++ b/releasenotes/notes/deprecate-condition_c_if-9548e5522814fe9c.yaml @@ -1,12 +1,12 @@ --- deprecations_circuits: - | - Deprecated the :attr:`.Instruction.condition` and the - :meth:`.Instruction.c_if` method. This attribute and method will be removed - in Qiskit 2.0 and along with any other uses of this in the Qiskit data - model. This functionality has been superseded by the :class:`.IfElseOp` - which can be used to describe a classically condition in a circuit. For - example, a circuit using :attr:`.Instruction.condition` like:: + Deprecated the :attr:`.Instruction.condition` attribute and the + :meth:`.Instruction.c_if` method. They will be removed + in Qiskit 2.0, along with any uses in the Qiskit data + model. This functionality has been superseded by the :class:`.IfElseOp` class + which can be used to describe a classical condition in a circuit. For + example, a circuit using :meth:`.Instruction.c_if` like:: from qiskit.circuit import QuantumCircuit @@ -17,7 +17,7 @@ deprecations_circuits: qc.measure(0, 0) qc.measure(1, 1) - can be rewritten using as:: + can be rewritten as:: qc = QuantumCircuit(2, 2) qc.h(0) From d4c868a0bee5e2f6cb3c91c762f757e5a8faa0c2 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 7 Oct 2024 15:27:50 -0400 Subject: [PATCH 5/9] Use private attribute in py->rust conversion --- crates/circuit/src/circuit_instruction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/circuit/src/circuit_instruction.rs b/crates/circuit/src/circuit_instruction.rs index 62d8860dbf1d..63b30f43f86f 100644 --- a/crates/circuit/src/circuit_instruction.rs +++ b/crates/circuit/src/circuit_instruction.rs @@ -655,7 +655,7 @@ impl<'py> FromPyObject<'py> for OperationFromPython { ob.getattr(intern!(py, "label"))?.extract()?, duration, unit, - ob.getattr(intern!(py, "condition"))?.extract()?, + ob.getattr(intern!(py, "_condition"))?.extract()?, )) }; From 86914e8e559fa92e4d35188623e0af3ef5619e96 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 7 Oct 2024 17:17:01 -0400 Subject: [PATCH 6/9] Avoid deprecation warning in non-deprecated code This commit fixes some places in the code where we were using the deprecated functionality where it needed a path to persist the behavior or where the deprecation warning became a performance bottleneck. The code is updated accordingly and to catch issues like this in the future the warning filters are adjusted to be more selective. --- qiskit/circuit/controlflow/builder.py | 6 +- qiskit/circuit/controlflow/if_else.py | 18 ++++-- qiskit/circuit/controlflow/while_loop.py | 12 +++- qiskit/circuit/instruction.py | 15 +++-- qiskit/circuit/quantumcircuit.py | 8 +-- qiskit/converters/circuit_to_instruction.py | 2 +- qiskit/dagcircuit/collect_blocks.py | 6 +- qiskit/dagcircuit/dagdependency.py | 6 +- qiskit/dagcircuit/dagnode.py | 4 +- qiskit/primitives/statevector_sampler.py | 2 +- qiskit/qasm2/export.py | 8 ++- qiskit/qasm2/parse.py | 2 +- qiskit/qasm3/exporter.py | 4 +- qiskit/qpy/binary_io/circuits.py | 6 +- .../operators/symplectic/clifford_circuits.py | 2 +- .../passes/basis/basis_translator.py | 4 +- .../passes/optimization/collect_cliffords.py | 2 +- .../optimization/collect_linear_functions.py | 2 +- .../optimization/collect_multiqubit_blocks.py | 4 +- .../commutative_inverse_cancellation.py | 2 +- .../passes/optimization/optimize_1q_gates.py | 2 +- .../optimize_swap_before_measure.py | 2 +- .../template_matching/backward_match.py | 8 +-- .../template_matching/forward_match.py | 8 +-- .../passes/routing/star_prerouting.py | 4 +- qiskit/visualization/circuit/_utils.py | 4 +- .../classical/test_expr_constructors.py | 64 ++++++++++--------- .../circuit/library/test_permutation.py | 3 +- test/python/circuit/library/test_qft.py | 3 +- .../python/circuit/test_circuit_properties.py | 51 +++++++++++---- test/python/circuit/test_compose.py | 63 ++++++++++++------ test/python/circuit/test_control_flow.py | 5 +- .../circuit/test_extensions_standard.py | 6 +- .../python/circuit/test_instruction_repeat.py | 9 ++- test/python/circuit/test_instructions.py | 50 ++++++++++----- test/python/circuit/test_random_circuit.py | 4 +- test/python/circuit/test_singleton.py | 35 ++++++---- test/python/compiler/test_transpiler.py | 3 +- .../converters/test_circuit_to_instruction.py | 13 ++-- test/python/dagcircuit/test_collect_blocks.py | 24 ++++--- test/python/dagcircuit/test_dagcircuit.py | 23 ++++--- test/python/qasm2/test_arxiv_examples.py | 6 +- test/python/qasm2/test_structure.py | 9 ++- .../python/transpiler/test_clifford_passes.py | 3 +- .../test_linear_functions_passes.py | 3 +- .../transpiler/test_unroll_3q_or_more.py | 3 +- test/utils/base.py | 9 ++- 47 files changed, 331 insertions(+), 201 deletions(-) diff --git a/qiskit/circuit/controlflow/builder.py b/qiskit/circuit/controlflow/builder.py index ab464a50ca67..458fabec49ef 100644 --- a/qiskit/circuit/controlflow/builder.py +++ b/qiskit/circuit/controlflow/builder.py @@ -284,7 +284,7 @@ def _copy_mutable_properties(self, instruction: Instruction) -> Instruction: The same instruction instance that was passed, but mutated to propagate the tracked changes to this class. """ - instruction.condition = self.condition + instruction._condition = self._condition return instruction # Provide some better error messages, just in case something goes wrong during development and @@ -639,8 +639,8 @@ def update_registers(index, op): # a register is already present, so we use our own tracking. self.add_register(register) out.add_register(register) - if getattr(op, "condition", None) is not None: - for register in condition_resources(op.condition).cregs: + if getattr(op, "_condition", None) is not None: + for register in condition_resources(op._condition).cregs: if register not in self.registers: self.add_register(register) out.add_register(register) diff --git a/qiskit/circuit/controlflow/if_else.py b/qiskit/circuit/controlflow/if_else.py index dd639c65f4b5..f5cdc9a2e1ae 100644 --- a/qiskit/circuit/controlflow/if_else.py +++ b/qiskit/circuit/controlflow/if_else.py @@ -87,12 +87,20 @@ def __init__( super().__init__("if_else", num_qubits, num_clbits, [true_body, false_body], label=label) - self.condition = validate_condition(condition) + self._condition = validate_condition(condition) @property def params(self): return self._params + @property + def condition(self): + return self._condition + + @condition.setter + def condition(self, value): + self._condition = value + @params.setter def params(self, parameters): # pylint: disable=cyclic-import @@ -152,7 +160,7 @@ def replace_blocks(self, blocks: Iterable[QuantumCircuit]) -> "IfElseOp": true_body, false_body = ( ablock for ablock, _ in itertools.zip_longest(blocks, range(2), fillvalue=None) ) - return IfElseOp(self.condition, true_body, false_body=false_body, label=self.label) + return IfElseOp(self._condition, true_body, false_body=false_body, label=self.label) def c_if(self, classical, val): raise NotImplementedError( @@ -200,7 +208,7 @@ def __init__( "if_else", len(self.__resources.qubits), len(self.__resources.clbits), [], label=label ) # Set the condition after super().__init__() has initialized it to None. - self.condition = validate_condition(condition) + self._condition = validate_condition(condition) def with_false_block(self, false_block: ControlFlowBuilderBlock) -> "IfElsePlaceholder": """Return a new placeholder instruction, with the false block set to the given value, @@ -225,7 +233,7 @@ def with_false_block(self, false_block: ControlFlowBuilderBlock) -> "IfElsePlace false_bits = false_block.qubits() | false_block.clbits() true_block.add_bits(false_bits - true_bits) false_block.add_bits(true_bits - false_bits) - return type(self)(self.condition, true_block, false_block, label=self.label) + return type(self)(self._condition, true_block, false_block, label=self.label) def registers(self): """Get the registers used by the interior blocks.""" @@ -288,7 +296,7 @@ def concrete_instruction(self, qubits, clbits): ) return ( self._copy_mutable_properties( - IfElseOp(self.condition, true_body, false_body, label=self.label) + IfElseOp(self._condition, true_body, false_body, label=self.label) ), InstructionResources( qubits=tuple(true_body.qubits), diff --git a/qiskit/circuit/controlflow/while_loop.py b/qiskit/circuit/controlflow/while_loop.py index 488a7a8b7025..7dcc2c22f6ee 100644 --- a/qiskit/circuit/controlflow/while_loop.py +++ b/qiskit/circuit/controlflow/while_loop.py @@ -53,12 +53,20 @@ def __init__( num_clbits = body.num_clbits super().__init__("while_loop", num_qubits, num_clbits, [body], label=label) - self.condition = validate_condition(condition) + self._condition = validate_condition(condition) @property def params(self): return self._params + @property + def condition(self): + return self._condition + + @condition.setter + def condition(self, value): + self._condition = value + @params.setter def params(self, parameters): # pylint: disable=cyclic-import @@ -88,7 +96,7 @@ def blocks(self): def replace_blocks(self, blocks): (body,) = blocks - return WhileLoopOp(self.condition, body, label=self.label) + return WhileLoopOp(self._condition, body, label=self.label) def c_if(self, classical, val): raise NotImplementedError( diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py index d8737eb477f8..e1df54c2cb99 100644 --- a/qiskit/circuit/instruction.py +++ b/qiskit/circuit/instruction.py @@ -411,8 +411,8 @@ def _assemble(self): # Add condition parameters for assembler. This is needed to convert # to a qobj conditional instruction at assemble time and after # conversion will be deleted by the assembler. - if self.condition: - instruction._condition = self.condition + if self._condition: + instruction._condition = self._condition return instruction @property @@ -634,7 +634,7 @@ def repeat(self, n): qargs = tuple(qc.qubits) cargs = tuple(qc.clbits) base = self.copy() - if self.condition: + if self._condition: # Condition is handled on the outer instruction. base = base.to_mutable() base.condition = None @@ -642,18 +642,19 @@ def repeat(self, n): qc._append(CircuitInstruction(base, qargs, cargs)) instruction.definition = qc - if self.condition: - instruction = instruction.c_if(*self.condition) + if self._condition: + instruction = instruction.c_if(*self._condition) return instruction @property + @deprecate_func(since="1.3.0", removal_timeline="in 2.0.0", is_property=True) def condition_bits(self) -> List[Clbit]: """Get Clbits in condition.""" from qiskit.circuit.controlflow import condition_resources # pylint: disable=cyclic-import - if self.condition is None: + if self._condition is None: return [] - return list(condition_resources(self.condition).clbits) + return list(condition_resources(self._condition).clbits) @property def name(self): diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py index 0a151a8476e3..d40dda54cf6f 100644 --- a/qiskit/circuit/quantumcircuit.py +++ b/qiskit/circuit/quantumcircuit.py @@ -2079,14 +2079,14 @@ def map_vars(op): is_control_flow = isinstance(n_op, ControlFlowOp) if ( not is_control_flow - and (condition := getattr(n_op, "condition", None)) is not None + and (condition := getattr(n_op, "_condition", None)) is not None ): n_op = n_op.copy() if n_op is op and copy else n_op n_op.condition = variable_mapper.map_condition(condition) elif is_control_flow: n_op = n_op.replace_blocks(recurse_block(block) for block in n_op.blocks) if isinstance(n_op, (IfElseOp, WhileLoopOp)): - n_op.condition = variable_mapper.map_condition(n_op.condition) + n_op.condition = variable_mapper.map_condition(n_op._condition) elif isinstance(n_op, SwitchCaseOp): n_op.target = variable_mapper.map_target(n_op.target) elif isinstance(n_op, Store): @@ -3504,7 +3504,7 @@ def update_from_expr(objects, node): for instruction in self._data: objects = set(itertools.chain(instruction.qubits, instruction.clbits)) - if (condition := getattr(instruction.operation, "condition", None)) is not None: + if (condition := getattr(instruction.operation, "_condition", None)) is not None: objects.update(_builder_utils.condition_resources(condition).clbits) if isinstance(condition, expr.Expr): update_from_expr(objects, condition) @@ -3607,7 +3607,7 @@ def num_connected_components(self, unitary_only: bool = False) -> int: else: args = instruction.qubits + instruction.clbits num_qargs = len(args) + ( - 1 if getattr(instruction.operation, "condition", None) else 0 + 1 if getattr(instruction.operation, "_condition", None) else 0 ) if num_qargs >= 2 and not getattr(instruction.operation, "_directive", False): diff --git a/qiskit/converters/circuit_to_instruction.py b/qiskit/converters/circuit_to_instruction.py index 4487a65e08fd..db314eee267e 100644 --- a/qiskit/converters/circuit_to_instruction.py +++ b/qiskit/converters/circuit_to_instruction.py @@ -125,7 +125,7 @@ def fix_condition(op): if (out := operation_map.get(original_id)) is not None: return out - condition = getattr(op, "condition", None) + condition = getattr(op, "_condition", None) if condition: reg, val = condition if isinstance(reg, Clbit): diff --git a/qiskit/dagcircuit/collect_blocks.py b/qiskit/dagcircuit/collect_blocks.py index c5c7b49144f7..99c51d2e3600 100644 --- a/qiskit/dagcircuit/collect_blocks.py +++ b/qiskit/dagcircuit/collect_blocks.py @@ -306,7 +306,7 @@ def split_block_into_layers(block: list[DAGOpNode | DAGDepNode]): cur_bits = set(node.qargs) cur_bits.update(node.cargs) - cond = getattr(node.op, "condition", None) + cond = getattr(node.op, "_condition", None) if cond is not None: cur_bits.update(condition_resources(cond).clbits) @@ -356,7 +356,7 @@ def collapse_to_operation(self, blocks, collapse_fn): for node in block: cur_qubits.update(node.qargs) cur_clbits.update(node.cargs) - cond = getattr(node.op, "condition", None) + cond = getattr(node.op, "_condition", None) if cond is not None: cur_clbits.update(condition_resources(cond).clbits) if isinstance(cond[0], ClassicalRegister): @@ -378,7 +378,7 @@ def collapse_to_operation(self, blocks, collapse_fn): for node in block: instructions = qc.append(CircuitInstruction(node.op, node.qargs, node.cargs)) - cond = getattr(node.op, "condition", None) + cond = getattr(node.op, "_condition", None) if cond is not None: instructions.c_if(*cond) diff --git a/qiskit/dagcircuit/dagdependency.py b/qiskit/dagcircuit/dagdependency.py index 63b91f920631..b37e7c28f1e7 100644 --- a/qiskit/dagcircuit/dagdependency.py +++ b/qiskit/dagcircuit/dagdependency.py @@ -396,13 +396,13 @@ def _create_op_node(self, operation, qargs, cargs): for elem in qargs: qindices_list.append(self.qubits.index(elem)) - if getattr(operation, "condition", None): + if getattr(operation, "_condition", None): # The change to handling operation.condition follows code patterns in quantum_circuit.py. # However: # (1) cindices_list are specific to template optimization and should not be computed # in this place. # (2) Template optimization pass needs currently does not handle general conditions. - cond_bits = condition_resources(operation.condition).clbits + cond_bits = condition_resources(operation._condition).clbits cindices_list = [self.clbits.index(clbit) for clbit in cond_bits] else: cindices_list = [] @@ -596,7 +596,7 @@ def replace_block_with_op(self, node_block, op, wire_pos_map, cycle_check=True): for nd in node_block: block_qargs |= set(nd.qargs) block_cargs |= set(nd.cargs) - cond = getattr(nd.op, "condition", None) + cond = getattr(nd.op, "_condition", None) if cond is not None: block_cargs.update(condition_resources(cond).clbits) diff --git a/qiskit/dagcircuit/dagnode.py b/qiskit/dagcircuit/dagnode.py index 60e2a7465707..151861d8028c 100644 --- a/qiskit/dagcircuit/dagnode.py +++ b/qiskit/dagcircuit/dagnode.py @@ -92,8 +92,8 @@ def key(var): def _condition_op_eq(node1, node2, bit_indices1, bit_indices2): - cond1 = node1.op.condition - cond2 = node2.op.condition + cond1 = node1.condition + cond2 = node2.condition if isinstance(cond1, expr.Expr) and isinstance(cond2, expr.Expr): if not expr.structurally_equivalent( cond1, cond2, _make_expr_key(bit_indices1), _make_expr_key(bit_indices2) diff --git a/qiskit/primitives/statevector_sampler.py b/qiskit/primitives/statevector_sampler.py index 78672e441dc4..486f35bc9364 100644 --- a/qiskit/primitives/statevector_sampler.py +++ b/qiskit/primitives/statevector_sampler.py @@ -287,6 +287,6 @@ def _final_measurement_mapping(circuit: QuantumCircuit) -> dict[tuple[ClassicalR def _has_control_flow(circuit: QuantumCircuit) -> bool: return any( - isinstance((op := instruction.operation), ControlFlowOp) or op.condition + isinstance((op := instruction.operation), ControlFlowOp) or op._condition for instruction in circuit ) diff --git a/qiskit/qasm2/export.py b/qiskit/qasm2/export.py index 3cf0d8942553..7c655dfd432b 100644 --- a/qiskit/qasm2/export.py +++ b/qiskit/qasm2/export.py @@ -246,12 +246,14 @@ def _instruction_call_site(operation): if operation.params: params = ",".join([pi_check(i, output="qasm", eps=1e-12) for i in operation.params]) qasm2_call = f"{qasm2_call}({params})" - if operation.condition is not None: - if not isinstance(operation.condition[0], ClassicalRegister): + if operation._condition is not None: + if not isinstance(operation._condition[0], ClassicalRegister): raise QASM2ExportError( "OpenQASM 2 can only condition on registers, but got '{operation.condition[0]}'" ) - qasm2_call = f"if({operation.condition[0].name}=={operation.condition[1]:d}) " + qasm2_call + qasm2_call = ( + f"if({operation._condition[0].name}=={operation._condition[1]:d}) " + qasm2_call + ) return qasm2_call diff --git a/qiskit/qasm2/parse.py b/qiskit/qasm2/parse.py index 746866a52f8b..39ff388b6b45 100644 --- a/qiskit/qasm2/parse.py +++ b/qiskit/qasm2/parse.py @@ -371,7 +371,7 @@ def __array__(self, dtype=None, copy=None): # to pickle ourselves, we just eagerly create the definition and pickle that. def __getstate__(self): - return (self.name, self.num_qubits, self.params, self.definition, self.condition) + return (self.name, self.num_qubits, self.params, self.definition, self._condition) def __setstate__(self, state): name, num_qubits, params, definition, condition = state diff --git a/qiskit/qasm3/exporter.py b/qiskit/qasm3/exporter.py index c76e296d6bab..246b7e8908a8 100644 --- a/qiskit/qasm3/exporter.py +++ b/qiskit/qasm3/exporter.py @@ -988,13 +988,13 @@ def build_current_scope(self) -> List[ast.Statement]: f" but received '{instruction.operation}'" ) - if instruction.operation.condition is None: + if instruction.operation._condition is None: statements.extend(nodes) else: body = ast.ProgramBlock(nodes) statements.append( ast.BranchingStatement( - self.build_expression(_lift_condition(instruction.operation.condition)), + self.build_expression(_lift_condition(instruction.operation._condition)), body, ) ) diff --git a/qiskit/qpy/binary_io/circuits.py b/qiskit/qpy/binary_io/circuits.py index a9aa46f7dce2..70c11bea1d27 100644 --- a/qiskit/qpy/binary_io/circuits.py +++ b/qiskit/qpy/binary_io/circuits.py @@ -774,13 +774,13 @@ def _write_instruction( condition_type = type_keys.Condition.NONE condition_register = b"" condition_value = 0 - if (op_condition := getattr(instruction.operation, "condition", None)) is not None: + if (op_condition := getattr(instruction.operation, "_condition", None)) is not None: if isinstance(op_condition, expr.Expr): condition_type = type_keys.Condition.EXPRESSION else: condition_type = type_keys.Condition.TWO_TUPLE - condition_register = _dumps_register(instruction.operation.condition[0], index_map) - condition_value = int(instruction.operation.condition[1]) + condition_register = _dumps_register(instruction.operation._condition[0], index_map) + condition_value = int(instruction.operation._condition[1]) gate_class_name = gate_class_name.encode(common.ENCODE) label = getattr(instruction.operation, "label", None) diff --git a/qiskit/quantum_info/operators/symplectic/clifford_circuits.py b/qiskit/quantum_info/operators/symplectic/clifford_circuits.py index 283893e59f58..44bcd31e2da5 100644 --- a/qiskit/quantum_info/operators/symplectic/clifford_circuits.py +++ b/qiskit/quantum_info/operators/symplectic/clifford_circuits.py @@ -81,7 +81,7 @@ def _append_operation(clifford, operation, qargs=None): else: # assert isinstance(gate, Instruction) name = gate.name - if getattr(gate, "condition", None) is not None: + if getattr(gate, "_condition", None) is not None: raise QiskitError("Conditional gate is not a valid Clifford operation.") # Apply gate if it is a Clifford basis gate diff --git a/qiskit/transpiler/passes/basis/basis_translator.py b/qiskit/transpiler/passes/basis/basis_translator.py index 01cbd18c0ee7..763a7e2a9d82 100644 --- a/qiskit/transpiler/passes/basis/basis_translator.py +++ b/qiskit/transpiler/passes/basis/basis_translator.py @@ -372,9 +372,9 @@ def _replace_node(self, dag, node, instr_map): new_node.op = new_node.op.copy() # dag_op may be the same instance as other ops in the dag, # so if there is a condition, need to copy - if getattr(node.op, "condition", None): + if getattr(node, "condition", None): new_node_op = new_node.op.to_mutable() - new_node_op.condition = node.op.condition + new_node_op.condition = node.condition new_node.op = new_node_op dag._apply_op_node_back(new_node) if target_dag.global_phase: diff --git a/qiskit/transpiler/passes/optimization/collect_cliffords.py b/qiskit/transpiler/passes/optimization/collect_cliffords.py index 5ee75af98000..8b26d04045c1 100644 --- a/qiskit/transpiler/passes/optimization/collect_cliffords.py +++ b/qiskit/transpiler/passes/optimization/collect_cliffords.py @@ -83,7 +83,7 @@ def __init__( def _is_clifford_gate(node, matrix_based=False): """Specifies whether a node holds a clifford gate.""" - if getattr(node.op, "condition", None) is not None: + if getattr(node.op, "_condition", None) is not None: return False if node.op.name in clifford_gate_names: return True diff --git a/qiskit/transpiler/passes/optimization/collect_linear_functions.py b/qiskit/transpiler/passes/optimization/collect_linear_functions.py index 158440e6a8a1..25a66e2bf9dc 100644 --- a/qiskit/transpiler/passes/optimization/collect_linear_functions.py +++ b/qiskit/transpiler/passes/optimization/collect_linear_functions.py @@ -71,7 +71,7 @@ def __init__( def _is_linear_gate(node): """Specifies whether a node holds a linear gate.""" - return node.op.name in ("cx", "swap") and getattr(node.op, "condition", None) is None + return node.op.name in ("cx", "swap") and getattr(node, "condition", None) is None def _collapse_to_linear_function(circuit): diff --git a/qiskit/transpiler/passes/optimization/collect_multiqubit_blocks.py b/qiskit/transpiler/passes/optimization/collect_multiqubit_blocks.py index e0dd61ff6cf4..34d51a17fe4a 100644 --- a/qiskit/transpiler/passes/optimization/collect_multiqubit_blocks.py +++ b/qiskit/transpiler/passes/optimization/collect_multiqubit_blocks.py @@ -120,7 +120,7 @@ def collect_key(x): if not isinstance(x, DAGOpNode): return "d" if isinstance(x.op, Gate): - if x.op.is_parameterized() or getattr(x.op, "condition", None) is not None: + if x.op.is_parameterized() or getattr(x.op, "_condition", None) is not None: return "c" return "b" + chr(ord("a") + len(x.qargs)) return "d" @@ -133,7 +133,7 @@ def collect_key(x): # check if the node is a gate and if it is parameterized if ( - getattr(nd.op, "condition", None) is not None + getattr(nd.op, "_condition", None) is not None or nd.op.is_parameterized() or not isinstance(nd.op, Gate) ): diff --git a/qiskit/transpiler/passes/optimization/commutative_inverse_cancellation.py b/qiskit/transpiler/passes/optimization/commutative_inverse_cancellation.py index 97324e2376cd..8c1b67b595a0 100644 --- a/qiskit/transpiler/passes/optimization/commutative_inverse_cancellation.py +++ b/qiskit/transpiler/passes/optimization/commutative_inverse_cancellation.py @@ -49,7 +49,7 @@ def _skip_node(self, node): # checking can be extended to cover additional cases. if getattr(node.op, "_directive", False) or node.name in {"measure", "reset", "delay"}: return True - if getattr(node.op, "condition", None): + if getattr(node, "condition", None): return True if node.op.is_parameterized(): return True diff --git a/qiskit/transpiler/passes/optimization/optimize_1q_gates.py b/qiskit/transpiler/passes/optimization/optimize_1q_gates.py index f8302b9232c0..466bcc4d5fcc 100644 --- a/qiskit/transpiler/passes/optimization/optimize_1q_gates.py +++ b/qiskit/transpiler/passes/optimization/optimize_1q_gates.py @@ -86,7 +86,7 @@ def run(self, dag): for current_node in run: left_name = current_node.name if ( - getattr(current_node.op, "condition", None) is not None + getattr(current_node, "condition", None) is not None or len(current_node.qargs) != 1 or left_name not in ["p", "u1", "u2", "u3", "u", "id"] ): diff --git a/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py b/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py index 6682e7ebbdba..7345a5bc40fd 100644 --- a/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py +++ b/qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py @@ -40,7 +40,7 @@ def run(self, dag): swaps = dag.op_nodes(SwapGate) for swap in swaps[::-1]: - if getattr(swap.op, "condition", None) is not None: + if getattr(swap.op, "_condition", None) is not None: continue final_successor = [] for successor in dag.descendants(swap): diff --git a/qiskit/transpiler/passes/optimization/template_matching/backward_match.py b/qiskit/transpiler/passes/optimization/template_matching/backward_match.py index d194d1cbbddf..7dffe38b9804 100644 --- a/qiskit/transpiler/passes/optimization/template_matching/backward_match.py +++ b/qiskit/transpiler/passes/optimization/template_matching/backward_match.py @@ -304,15 +304,15 @@ def _is_same_c_conf(self, node_circuit, node_template, carg_circuit): """ if ( node_circuit.type == "op" - and getattr(node_circuit.op, "condition", None) + and getattr(node_circuit.op, "_condition", None) and node_template.type == "op" - and getattr(node_template.op, "condition", None) + and getattr(node_template.op, "_condition", None) ): if set(carg_circuit) != set(node_template.cindices): return False if ( - getattr(node_circuit.op, "condition", None)[1] - != getattr(node_template.op, "condition", None)[1] + getattr(node_circuit.op, "_condition", None)[1] + != getattr(node_template.op, "_condition", None)[1] ): return False return True diff --git a/qiskit/transpiler/passes/optimization/template_matching/forward_match.py b/qiskit/transpiler/passes/optimization/template_matching/forward_match.py index d8dd5bb2b9a2..b4232d8ff6d2 100644 --- a/qiskit/transpiler/passes/optimization/template_matching/forward_match.py +++ b/qiskit/transpiler/passes/optimization/template_matching/forward_match.py @@ -311,15 +311,15 @@ def _is_same_c_conf(self, node_circuit, node_template): """ if ( node_circuit.type == "op" - and getattr(node_circuit.op, "condition", None) + and getattr(node_circuit.op, "_condition", None) and node_template.type == "op" - and getattr(node_template.op, "condition", None) + and getattr(node_template.op, "_condition", None) ): if set(self.carg_indices) != set(node_template.cindices): return False if ( - getattr(node_circuit.op, "condition", None)[1] - != getattr(node_template.op, "condition", None)[1] + getattr(node_circuit.op, "_condition", None)[1] + != getattr(node_template.op, "_condition", None)[1] ): return False return True diff --git a/qiskit/transpiler/passes/routing/star_prerouting.py b/qiskit/transpiler/passes/routing/star_prerouting.py index 53bc971a268b..259d79ba636b 100644 --- a/qiskit/transpiler/passes/routing/star_prerouting.py +++ b/qiskit/transpiler/passes/routing/star_prerouting.py @@ -223,7 +223,7 @@ def filter_fn(node): return ( len(node.qargs) <= 2 and len(node.cargs) == 0 - and getattr(node.op, "condition", None) is None + and getattr(node, "condition", None) is None and not isinstance(node.op, Barrier) ) @@ -372,7 +372,7 @@ def _extract_nodes(nodes, dag): qubit_indices = [dag.find_bit(qubit).index for qubit in node.qargs] classical_bit_indices = set() - if node.op.condition is not None: + if node.condition is not None: classical_bit_indices.update(condition_resources(node.op.condition).clbits) if isinstance(node.op, SwitchCaseOp): diff --git a/qiskit/visualization/circuit/_utils.py b/qiskit/visualization/circuit/_utils.py index d933d38b5c4a..0aa6e7c10188 100644 --- a/qiskit/visualization/circuit/_utils.py +++ b/qiskit/visualization/circuit/_utils.py @@ -503,7 +503,7 @@ def _get_gate_span(qubits, node): # type of op must be the only op in the layer if isinstance(node.op, ControlFlowOp): span = qubits - elif node.cargs or getattr(node.op, "condition", None): + elif node.cargs or getattr(node, "condition", None): span = qubits[min_index : len(qubits)] else: span = qubits[min_index : max_index + 1] @@ -582,7 +582,7 @@ def slide_from_left(self, node, index): curr_index = index last_insertable_index = -1 index_stop = -1 - if (condition := getattr(node.op, "condition", None)) is not None: + if (condition := getattr(node, "condition", None)) is not None: index_stop = max( (self.measure_map[bit] for bit in condition_resources(condition).clbits), default=index_stop, diff --git a/test/python/circuit/classical/test_expr_constructors.py b/test/python/circuit/classical/test_expr_constructors.py index 2be64fbef9f3..6500e5593ac2 100644 --- a/test/python/circuit/classical/test_expr_constructors.py +++ b/test/python/circuit/classical/test_expr_constructors.py @@ -28,48 +28,52 @@ def test_lift_legacy_condition(self): inst = Instruction("custom", 1, 0, []) with self.assertWarns(DeprecationWarning): inst.c_if(cr, 7) - self.assertEqual( - expr.lift_legacy_condition(inst.condition), - expr.Binary( - expr.Binary.Op.EQUAL, - expr.Var(cr, types.Uint(cr.size)), - expr.Value(7, types.Uint(cr.size)), - types.Bool(), - ), - ) + with self.assertWarns(DeprecationWarning): + self.assertEqual( + expr.lift_legacy_condition(inst.condition), + expr.Binary( + expr.Binary.Op.EQUAL, + expr.Var(cr, types.Uint(cr.size)), + expr.Value(7, types.Uint(cr.size)), + types.Bool(), + ), + ) inst = Instruction("custom", 1, 0, []) with self.assertWarns(DeprecationWarning): inst.c_if(cr, 255) - self.assertEqual( - expr.lift_legacy_condition(inst.condition), - expr.Binary( - expr.Binary.Op.EQUAL, - expr.Cast(expr.Var(cr, types.Uint(cr.size)), types.Uint(8), implicit=True), - expr.Value(255, types.Uint(8)), - types.Bool(), - ), - ) + with self.assertWarns(DeprecationWarning): + self.assertEqual( + expr.lift_legacy_condition(inst.condition), + expr.Binary( + expr.Binary.Op.EQUAL, + expr.Cast(expr.Var(cr, types.Uint(cr.size)), types.Uint(8), implicit=True), + expr.Value(255, types.Uint(8)), + types.Bool(), + ), + ) inst = Instruction("custom", 1, 0, []) with self.assertWarns(DeprecationWarning): inst.c_if(clbit, False) - self.assertEqual( - expr.lift_legacy_condition(inst.condition), - expr.Unary( - expr.Unary.Op.LOGIC_NOT, - expr.Var(clbit, types.Bool()), - types.Bool(), - ), - ) + with self.assertWarns(DeprecationWarning): + self.assertEqual( + expr.lift_legacy_condition(inst.condition), + expr.Unary( + expr.Unary.Op.LOGIC_NOT, + expr.Var(clbit, types.Bool()), + types.Bool(), + ), + ) inst = Instruction("custom", 1, 0, []) with self.assertWarns(DeprecationWarning): inst.c_if(clbit, True) - self.assertEqual( - expr.lift_legacy_condition(inst.condition), - expr.Var(clbit, types.Bool()), - ) + with self.assertWarns(DeprecationWarning): + self.assertEqual( + expr.lift_legacy_condition(inst.condition), + expr.Var(clbit, types.Bool()), + ) def test_value_lifts_qiskit_scalars(self): cr = ClassicalRegister(3, "c") diff --git a/test/python/circuit/library/test_permutation.py b/test/python/circuit/library/test_permutation.py index 34ae89ce4aa4..380a5f6a0358 100644 --- a/test/python/circuit/library/test_permutation.py +++ b/test/python/circuit/library/test_permutation.py @@ -151,7 +151,8 @@ def test_conditional(self): qc = QuantumCircuit(5, 1) with self.assertWarns(DeprecationWarning): qc.append(PermutationGate([1, 2, 0]), [2, 3, 4]).c_if(0, 1) - self.assertIsNotNone(qc.data[0].operation.condition) + with self.assertWarns(DeprecationWarning): + self.assertIsNotNone(qc.data[0].operation.condition) def test_qasm(self): """Test qasm for circuits with permutations.""" diff --git a/test/python/circuit/library/test_qft.py b/test/python/circuit/library/test_qft.py index 77510f3cfb9c..6250b656a772 100644 --- a/test/python/circuit/library/test_qft.py +++ b/test/python/circuit/library/test_qft.py @@ -274,7 +274,8 @@ def test_conditional(self): qc = QuantumCircuit(5, 1) with self.assertWarns(DeprecationWarning): qc.append(QFTGate(4), [1, 2, 0, 4]).c_if(0, 1) - self.assertIsNotNone(qc.data[0].operation.condition) + with self.assertWarns(DeprecationWarning): + self.assertIsNotNone(qc.data[0].operation.condition) def test_qasm(self): """Test qasm for circuits with QFTGates.""" diff --git a/test/python/circuit/test_circuit_properties.py b/test/python/circuit/test_circuit_properties.py index fa9e6e5b169d..82758dbb9664 100644 --- a/test/python/circuit/test_circuit_properties.py +++ b/test/python/circuit/test_circuit_properties.py @@ -626,10 +626,13 @@ def test_circuit_depth_multiqubit_or_conditional(self): circ.measure(1, 0) with self.assertWarns(DeprecationWarning): circ.x(0).c_if(0, 1) - self.assertEqual( - circ.depth(lambda x: x.operation.num_qubits >= 2 or x.operation.condition is not None), - 4, - ) + with self.assertWarns(DeprecationWarning): + self.assertEqual( + circ.depth( + lambda x: x.operation.num_qubits >= 2 or x.operation.condition is not None + ), + 4, + ) def test_circuit_depth_first_qubit(self): """Test finding depth of gates touching q0 only.""" @@ -831,7 +834,9 @@ def test_circuit_connected_components_multi_reg(self): qc.cx(q1[1], q2[1]) qc.cx(q2[1], q1[2]) qc.cx(q1[2], q2[0]) - self.assertEqual(qc.num_connected_components(), 1) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 1) def test_circuit_connected_components_multi_reg2(self): """Test tensor factors works over multi registers #2.""" @@ -852,7 +857,9 @@ def test_circuit_connected_components_multi_reg2(self): qc.cx(q1[0], q2[1]) qc.cx(q2[0], q1[2]) qc.cx(q1[1], q2[0]) - self.assertEqual(qc.num_connected_components(), 2) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 2) def test_circuit_connected_components_disconnected(self): """Test tensor factors works with 2q subspaces.""" @@ -885,7 +892,9 @@ def test_circuit_connected_components_disconnected(self): qc.cx(q1[2], q2[2]) qc.cx(q1[3], q2[1]) qc.cx(q1[4], q2[0]) - self.assertEqual(qc.num_connected_components(), 5) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 5) def test_circuit_connected_components_with_clbits(self): """Test tensor components with classical register.""" @@ -913,7 +922,9 @@ def test_circuit_connected_components_with_clbits(self): qc.measure(q[1], c[1]) qc.measure(q[2], c[2]) qc.measure(q[3], c[3]) - self.assertEqual(qc.num_connected_components(), 4) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 4) def test_circuit_connected_components_with_cond(self): """Test tensor components with one conditional gate.""" @@ -944,7 +955,9 @@ def test_circuit_connected_components_with_cond(self): qc.measure(q[1], c[1]) qc.measure(q[2], c[2]) qc.measure(q[3], c[3]) - self.assertEqual(qc.num_connected_components(), 1) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 1) def test_circuit_connected_components_with_cond2(self): """Test tensor components with two conditional gates.""" @@ -972,7 +985,9 @@ def test_circuit_connected_components_with_cond2(self): qc.h(0).c_if(c, 0) with self.assertWarns(DeprecationWarning): qc.cx(1, 2).c_if(c, 4) - self.assertEqual(qc.num_connected_components(), 2) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 2) def test_circuit_connected_components_with_cond3(self): """Test tensor components with three conditional gates and measurements.""" @@ -1005,7 +1020,9 @@ def test_circuit_connected_components_with_cond3(self): qc.measure(q[2], c[2]) with self.assertWarns(DeprecationWarning): qc.x(q[3]).c_if(c, 2) - self.assertEqual(qc.num_connected_components(), 1) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 1) def test_circuit_connected_components_with_bit_cond(self): """Test tensor components with one single bit conditional gate.""" @@ -1036,7 +1053,9 @@ def test_circuit_connected_components_with_bit_cond(self): qc.measure(q[1], c[1]) qc.measure(q[2], c[2]) qc.measure(q[3], c[3]) - self.assertEqual(qc.num_connected_components(), 3) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 3) def test_circuit_connected_components_with_bit_cond2(self): """Test tensor components with two bit conditional gates.""" @@ -1066,7 +1085,9 @@ def test_circuit_connected_components_with_bit_cond2(self): qc.cx(1, 0).c_if(c[4], False) with self.assertWarns(DeprecationWarning): qc.cz(2, 3).c_if(c[0], True) - self.assertEqual(qc.num_connected_components(), 5) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 5) def test_circuit_connected_components_with_bit_cond3(self): """Test tensor components with register and bit conditional gates.""" @@ -1097,7 +1118,9 @@ def test_circuit_connected_components_with_bit_cond3(self): qc.cx(q[1], q[2]).c_if(c, 1) with self.assertWarns(DeprecationWarning): qc.x(q[3]).c_if(c[2], True) - self.assertEqual(qc.num_connected_components(), 1) + # Internally calls op.condition_bits + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.num_connected_components(), 1) def test_circuit_unitary_factors1(self): """Test unitary factors empty circuit.""" diff --git a/test/python/circuit/test_compose.py b/test/python/circuit/test_compose.py index cc4be3c3ffa2..a16157ff1ae2 100644 --- a/test/python/circuit/test_compose.py +++ b/test/python/circuit/test_compose.py @@ -371,7 +371,8 @@ def __init__(self): conditional.append(Custom(), [0], []).c_if(conditional.clbits[0], True) test = base.compose(conditional, qubits=[0], clbits=[0], copy=False) self.assertIs(test.data[-1].operation, conditional.data[-1].operation) - self.assertEqual(test.data[-1].operation.condition, (test.clbits[0], True)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(test.data[-1].operation.condition, (test.clbits[0], True)) def test_compose_classical(self): """Composing on classical bits. @@ -504,16 +505,26 @@ def test_compose_conditional_no_match(self): # The registers should have been mapped, including the bits inside them. Unlike the # previous test, there are no matching registers in the destination circuit, so the # composition needs to add new registers (bit groupings) over the existing mapped bits. - self.assertIsNot(z.condition, None) - self.assertIsInstance(z.condition[0], ClassicalRegister) - self.assertEqual(len(z.condition[0]), len(right.cregs[0])) - self.assertIs(z.condition[0][0], test.clbits[0]) - self.assertEqual(z.condition[1], 1) - self.assertIsNot(x.condition, None) - self.assertIsInstance(x.condition[0], ClassicalRegister) - self.assertEqual(len(x.condition[0]), len(right.cregs[1])) - self.assertEqual(z.condition[1], 1) - self.assertIs(x.condition[0][0], test.clbits[1]) + with self.assertWarns(DeprecationWarning): + self.assertIsNot(z.condition, None) + with self.assertWarns(DeprecationWarning): + self.assertIsInstance(z.condition[0], ClassicalRegister) + with self.assertWarns(DeprecationWarning): + self.assertEqual(len(z.condition[0]), len(right.cregs[0])) + with self.assertWarns(DeprecationWarning): + self.assertIs(z.condition[0][0], test.clbits[0]) + with self.assertWarns(DeprecationWarning): + self.assertEqual(z.condition[1], 1) + with self.assertWarns(DeprecationWarning): + self.assertIsNot(x.condition, None) + with self.assertWarns(DeprecationWarning): + self.assertIsInstance(x.condition[0], ClassicalRegister) + with self.assertWarns(DeprecationWarning): + self.assertEqual(len(x.condition[0]), len(right.cregs[1])) + with self.assertWarns(DeprecationWarning): + self.assertEqual(z.condition[1], 1) + with self.assertWarns(DeprecationWarning): + self.assertIs(x.condition[0][0], test.clbits[1]) def test_compose_switch_match(self): """Test that composition containing a `switch` with a register that matches proceeds @@ -736,7 +747,8 @@ def test_single_bit_condition(self): test = QuantumCircuit(1, 1).compose(base) self.assertIsNot(base.clbits[0], test.clbits[0]) self.assertEqual(base, test) - self.assertIs(test.data[0].operation.condition[0], test.clbits[0]) + with self.assertWarns(DeprecationWarning): + self.assertIs(test.data[0].operation.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 @@ -757,10 +769,14 @@ def test_condition_mapping_ifelseop(self): bit_instruction = test.data[0].operation reg_instruction = test.data[1].operation - 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)) + with self.assertWarns(DeprecationWarning): + self.assertIs(bit_instruction.condition[0], test_loose) + with self.assertWarns(DeprecationWarning): + self.assertEqual(bit_instruction.condition, (test_loose, True)) + with self.assertWarns(DeprecationWarning): + self.assertIs(reg_instruction.condition[0], test_creg) + with self.assertWarns(DeprecationWarning): + 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 @@ -781,10 +797,14 @@ def test_condition_mapping_whileloopop(self): bit_instruction = test.data[0].operation reg_instruction = test.data[1].operation - 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)) + with self.assertWarns(DeprecationWarning): + self.assertIs(bit_instruction.condition[0], test_loose) + with self.assertWarns(DeprecationWarning): + self.assertEqual(bit_instruction.condition, (test_loose, True)) + with self.assertWarns(DeprecationWarning): + self.assertIs(reg_instruction.condition[0], test_creg) + with self.assertWarns(DeprecationWarning): + self.assertEqual(reg_instruction.condition, (test_creg, 3)) def test_compose_no_clbits_in_one(self): """Test combining a circuit with cregs to one without""" @@ -852,7 +872,8 @@ def test_expr_condition_is_mapped(self): # Check that the input conditions weren't mutated. for in_condition, instruction in zip((test_1, test_2, test_3), source.data): - self.assertEqual(in_condition(), instruction.operation.condition) + with self.assertWarns(DeprecationWarning): + self.assertEqual(in_condition(), instruction.operation.condition) # Should be `a_dest`, `b_dest` and an added one to account for `c_src`. self.assertEqual(len(dest.cregs), 3) diff --git a/test/python/circuit/test_control_flow.py b/test/python/circuit/test_control_flow.py index cdb87ac0d342..51733a37ffc9 100644 --- a/test/python/circuit/test_control_flow.py +++ b/test/python/circuit/test_control_flow.py @@ -587,7 +587,8 @@ def test_appending_switch_case_op(self, target, labels): self.assertEqual(qc.data[0].operation.name, "switch_case") self.assertEqual(qc.data[0].operation.params, bodies[: len(labels)]) - self.assertEqual(qc.data[0].operation.condition, None) + with self.assertWarns(DeprecationWarning): + self.assertEqual(qc.data[0].operation.condition, None) self.assertEqual(qc.data[0].qubits, tuple(qc.qubits[1:4])) self.assertEqual(qc.data[0].clbits, (qc.clbits[1],)) @@ -618,7 +619,7 @@ def test_quantumcircuit_switch(self, target, labels): self.assertEqual(qc.data[0].operation.name, "switch_case") self.assertEqual(qc.data[0].operation.params, bodies[: len(labels)]) - self.assertEqual(qc.data[0].operation.condition, None) + self.assertEqual(qc.data[0].operation._condition, None) self.assertEqual(qc.data[0].qubits, tuple(qc.qubits[1:4])) self.assertEqual(qc.data[0].clbits, (qc.clbits[1],)) diff --git a/test/python/circuit/test_extensions_standard.py b/test/python/circuit/test_extensions_standard.py index 042087baa6d9..d9bdbc3fd13f 100644 --- a/test/python/circuit/test_extensions_standard.py +++ b/test/python/circuit/test_extensions_standard.py @@ -136,14 +136,16 @@ def test_cif_reg(self): self.circuit.h(self.qr[0]).c_if(self.cr, 7) self.assertEqual(self.circuit[0].operation.name, "h") self.assertEqual(self.circuit[0].qubits, (self.qr[0],)) - self.assertEqual(self.circuit[0].operation.condition, (self.cr, 7)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(self.circuit[0].operation.condition, (self.cr, 7)) def test_cif_single_bit(self): with self.assertWarns(DeprecationWarning): self.circuit.h(self.qr[0]).c_if(self.cr[0], True) self.assertEqual(self.circuit[0].operation.name, "h") self.assertEqual(self.circuit[0].qubits, (self.qr[0],)) - self.assertEqual(self.circuit[0].operation.condition, (self.cr[0], True)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(self.circuit[0].operation.condition, (self.cr[0], True)) def test_crz(self): self.circuit.crz(1, self.qr[0], self.qr[1]) diff --git a/test/python/circuit/test_instruction_repeat.py b/test/python/circuit/test_instruction_repeat.py index 74142fa898e9..b2fcda1522e6 100644 --- a/test/python/circuit/test_instruction_repeat.py +++ b/test/python/circuit/test_instruction_repeat.py @@ -57,7 +57,8 @@ def test_conditional(self): cr = ClassicalRegister(3, "cr") with self.assertWarns(DeprecationWarning): gate = SGate().c_if(cr, 7).repeat(5) - self.assertEqual(gate.condition, (cr, 7)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(gate.condition, (cr, 7)) defn = QuantumCircuit(1) for _ in range(5): @@ -101,7 +102,8 @@ def test_conditional(self): cr = ClassicalRegister(3, "cr") with self.assertWarns(DeprecationWarning): gate = CXGate().c_if(cr, 7).repeat(5) - self.assertEqual(gate.condition, (cr, 7)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(gate.condition, (cr, 7)) defn = QuantumCircuit(2) for _ in range(5): @@ -149,7 +151,8 @@ def test_measure_conditional(self): cr = ClassicalRegister(3, "cr") with self.assertWarns(DeprecationWarning): measure = Measure().c_if(cr, 7).repeat(5) - self.assertEqual(measure.condition, (cr, 7)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(measure.condition, (cr, 7)) defn = QuantumCircuit(1, 1) for _ in range(5): diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index a412d09a2302..da1d0797870e 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -453,13 +453,18 @@ def key(bit): return body.find_bit(bit).index op = IfElseOp((bits[0], False), body) - self.assertEqual(op.condition_bits, [bits[0]]) + with self.assertWarns(DeprecationWarning): + self.assertEqual(op.condition_bits, [bits[0]]) op = IfElseOp((cr1, 3), body) - self.assertEqual(op.condition_bits, list(cr1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(op.condition_bits, list(cr1)) op = IfElseOp(expr.logic_and(bits[1], expr.equal(cr2, 3)), body) - self.assertEqual(sorted(op.condition_bits, key=key), sorted([bits[1]] + list(cr2), key=key)) + with self.assertWarns(DeprecationWarning): + self.assertEqual( + sorted(op.condition_bits, key=key), sorted([bits[1]] + list(cr2), key=key) + ) def test_instructionset_c_if_direct_resource(self): """Test that using :meth:`.InstructionSet.c_if` with an exact classical resource always @@ -476,7 +481,8 @@ def case(resource): qc = QuantumCircuit(cr1, qubits, loose_clbits, cr2, cr3) with self.assertWarns(DeprecationWarning): qc.x(0).c_if(resource, 0) - c_if_resource = qc.data[0].operation.condition[0] + with self.assertWarns(DeprecationWarning): + c_if_resource = qc.data[0].operation.condition[0] self.assertIs(c_if_resource, resource) with self.subTest("classical register"): @@ -511,7 +517,8 @@ def test_instructionset_c_if_indexing(self): with self.assertWarns(DeprecationWarning): qc.x(0).c_if(index, 0) qc.measure(0, index) - from_c_if = qc.data[-2].operation.condition[0] + with self.assertWarns(DeprecationWarning): + from_c_if = qc.data[-2].operation.condition[0] from_measure = qc.data[-1].clbits[0] self.assertIs(from_c_if, from_measure) # Sanity check that the bit is also the one we expected. @@ -527,15 +534,18 @@ def test_instructionset_c_if_size_1_classical_register(self): with self.subTest("classical register"): with self.assertWarns(DeprecationWarning): qc.x(0).c_if(cr, 0) - self.assertIs(qc.data[-1].operation.condition[0], cr) + with self.assertWarns(DeprecationWarning): + self.assertIs(qc.data[-1].operation.condition[0], cr) with self.subTest("classical bit by value"): with self.assertWarns(DeprecationWarning): qc.x(0).c_if(cr[0], 0) - self.assertIs(qc.data[-1].operation.condition[0], cr[0]) + with self.assertWarns(DeprecationWarning): + self.assertIs(qc.data[-1].operation.condition[0], cr[0]) with self.subTest("classical bit by index"): with self.assertWarns(DeprecationWarning): qc.x(0).c_if(0, 0) - self.assertIs(qc.data[-1].operation.condition[0], cr[0]) + with self.assertWarns(DeprecationWarning): + self.assertIs(qc.data[-1].operation.condition[0], cr[0]) def test_instructionset_c_if_no_classical_registers(self): """Test that using :meth:`.InstructionSet.c_if` works if there are no classical registers @@ -547,11 +557,13 @@ def test_instructionset_c_if_no_classical_registers(self): with self.subTest("by value"): with self.assertWarns(DeprecationWarning): qc.x(0).c_if(bits[1], 0) - self.assertIs(qc.data[-1].operation.condition[0], bits[1]) + with self.assertWarns(DeprecationWarning): + self.assertIs(qc.data[-1].operation.condition[0], bits[1]) with self.subTest("by index"): with self.assertWarns(DeprecationWarning): qc.x(0).c_if(0, 0) - self.assertIs(qc.data[-1].operation.condition[0], bits[1]) + with self.assertWarns(DeprecationWarning): + self.assertIs(qc.data[-1].operation.condition[0], bits[1]) def test_instructionset_c_if_rejects_invalid_specifiers(self): """Test that calling the :meth:`.InstructionSet.c_if` method on instructions added to a @@ -591,7 +603,8 @@ def test_instructionset_c_if_with_no_requester(self): register = ClassicalRegister(2) with self.assertWarns(DeprecationWarning): instructions.c_if(register, 0) - self.assertIs(instructions[0].operation.condition[0], register) + with self.assertWarns(DeprecationWarning): + self.assertIs(instructions[0].operation.condition[0], register) with self.subTest("accepts arbitrary bit"): instruction = RZGate(0) instructions = InstructionSet() @@ -599,7 +612,8 @@ def test_instructionset_c_if_with_no_requester(self): bit = Clbit() with self.assertWarns(DeprecationWarning): instructions.c_if(bit, 0) - self.assertIs(instructions[0].operation.condition[0], bit) + with self.assertWarns(DeprecationWarning): + self.assertIs(instructions[0].operation.condition[0], bit) with self.subTest("rejects index"): instruction = RZGate(0) instructions = InstructionSet() @@ -634,7 +648,8 @@ def dummy_requester(specifier): with self.assertWarns(DeprecationWarning): instructions.c_if(bit, 0) dummy_requester.assert_called_once_with(bit) - self.assertIs(instructions[0].operation.condition[0], sentinel_bit) + with self.assertWarns(DeprecationWarning): + self.assertIs(instructions[0].operation.condition[0], sentinel_bit) with self.subTest("calls requester with index"): dummy_requester.reset_mock() instruction = RZGate(0) @@ -644,7 +659,8 @@ def dummy_requester(specifier): with self.assertWarns(DeprecationWarning): instructions.c_if(index, 0) dummy_requester.assert_called_once_with(index) - self.assertIs(instructions[0].operation.condition[0], sentinel_bit) + with self.assertWarns(DeprecationWarning): + self.assertIs(instructions[0].operation.condition[0], sentinel_bit) with self.subTest("calls requester with register"): dummy_requester.reset_mock() instruction = RZGate(0) @@ -654,7 +670,8 @@ def dummy_requester(specifier): with self.assertWarns(DeprecationWarning): instructions.c_if(register, 0) dummy_requester.assert_called_once_with(register) - self.assertIs(instructions[0].operation.condition[0], sentinel_register) + with self.assertWarns(DeprecationWarning): + self.assertIs(instructions[0].operation.condition[0], sentinel_register) with self.subTest("calls requester only once when broadcast"): dummy_requester.reset_mock() instruction_list = [RZGate(0), RZGate(0), RZGate(0)] @@ -666,7 +683,8 @@ def dummy_requester(specifier): instructions.c_if(register, 0) dummy_requester.assert_called_once_with(register) for instruction in instruction_list: - self.assertIs(instructions[0].operation.condition[0], sentinel_register) + with self.assertWarns(DeprecationWarning): + self.assertIs(instructions[0].operation.condition[0], sentinel_register) def test_label_type_enforcement(self): """Test instruction label type enforcement.""" diff --git a/test/python/circuit/test_random_circuit.py b/test/python/circuit/test_random_circuit.py index 0d798fe8ad19..0845fdbe97d6 100644 --- a/test/python/circuit/test_random_circuit.py +++ b/test/python/circuit/test_random_circuit.py @@ -62,7 +62,7 @@ def test_large_conditional(self): # Test that at least one instruction had a condition generated. It's possible that this # fails due to very bad luck with the random seed - if so, change the seed to ensure that a # condition _is_ generated, because we need to test that generation doesn't error. - conditions = (getattr(instruction.operation, "condition", None) for instruction in circ) + conditions = (getattr(instruction.operation, "_condition", None) for instruction in circ) conditions = [x for x in conditions if x is not None] self.assertNotEqual(conditions, []) for register, value in conditions: @@ -82,7 +82,7 @@ def test_random_mid_circuit_measure_conditional(self): self.assertEqual(4, len(op_names)) self.assertEqual(["measure"] * num_qubits, op_names[1 : 1 + num_qubits]) conditions = [ - bool(getattr(instruction.operation, "condition", None)) for instruction in circ + bool(getattr(instruction.operation, "_condition", None)) for instruction in circ ] self.assertEqual([False, False, False, True], conditions) diff --git a/test/python/circuit/test_singleton.py b/test/python/circuit/test_singleton.py index 8a2b57235f06..396fdcc6a240 100644 --- a/test/python/circuit/test_singleton.py +++ b/test/python/circuit/test_singleton.py @@ -228,7 +228,8 @@ def test_condition_via_instructionset(self): with self.assertWarns(DeprecationWarning): circuit.h(qr[0]).c_if(cr, 1) self.assertIsNot(gate, circuit.data[0].operation) - self.assertEqual(circuit.data[0].operation.condition, (cr, 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(circuit.data[0].operation.condition, (cr, 1)) def test_is_mutable(self): gate = HGate() @@ -258,7 +259,8 @@ def test_to_mutable_setter(self): self.assertEqual(mutable_gate.label, "foo") self.assertEqual(mutable_gate.duration, 3) self.assertEqual(mutable_gate.unit, "s") - self.assertEqual(mutable_gate.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(mutable_gate.condition, (clbit, 0)) def test_to_mutable_of_mutable_instance(self): gate = HGate(label="foo") @@ -299,7 +301,8 @@ def test_mutable_pickle(self): with self.assertWarns(DeprecationWarning): condition_gate = gate.c_if(clbit, 0) self.assertIsNot(gate, condition_gate) - self.assertEqual(condition_gate.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(condition_gate.condition, (clbit, 0)) self.assertTrue(condition_gate.mutable) with io.BytesIO() as fd: pickle.dump(condition_gate, fd) @@ -577,7 +580,8 @@ def test_condition_label_copy(self): self.assertIsNot(gate, copied) self.assertEqual(gate, copied) self.assertEqual(copied.label, "conditionally special") - self.assertEqual(copied.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(copied.condition, (clbit, 0)) def test_deepcopy(self): gate = CXGate() @@ -611,7 +615,8 @@ def test_condition_label_deepcopy(self): self.assertIsNot(gate, copied) self.assertEqual(gate, copied) self.assertEqual(copied.label, "conditionally special") - self.assertEqual(copied.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(copied.condition, (clbit, 0)) def test_label_deepcopy_new(self): gate = CHGate() @@ -663,7 +668,8 @@ def test_round_trip_dag_conversion_with_condition(self): out = dag_to_circuit(dag) self.assertIsNot(qc.data[0].operation, out.data[0].operation) self.assertEqual(qc.data[0].operation, out.data[0].operation) - self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) def test_round_trip_dag_conversion_condition_label(self): qc = QuantumCircuit(2, 1) @@ -674,7 +680,8 @@ def test_round_trip_dag_conversion_condition_label(self): out = dag_to_circuit(dag) self.assertIsNot(qc.data[0].operation, out.data[0].operation) self.assertEqual(qc.data[0].operation, out.data[0].operation) - self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) self.assertEqual(out.data[0].operation.label, "conditionally special") def test_condition_via_instructionset(self): @@ -685,7 +692,7 @@ def test_condition_via_instructionset(self): with self.assertWarns(DeprecationWarning): circuit.h(qr[0]).c_if(cr, 1) self.assertIsNot(gate, circuit.data[0].operation) - self.assertEqual(circuit.data[0].operation.condition, (cr, 1)) + self.assertEqual(circuit.data[0].operation._condition, (cr, 1)) def test_is_mutable(self): gate = CXGate() @@ -715,7 +722,8 @@ def test_to_mutable_setter(self): self.assertEqual(mutable_gate.label, "foo") self.assertEqual(mutable_gate.duration, 3) self.assertEqual(mutable_gate.unit, "s") - self.assertEqual(mutable_gate.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(mutable_gate.condition, (clbit, 0)) def test_to_mutable_of_mutable_instance(self): gate = CZGate(label="foo") @@ -747,7 +755,8 @@ def test_inner_outer_label_with_c_if(self): self.assertTrue(conditonal_controlled_gate.mutable) self.assertEqual("my h gate", conditonal_controlled_gate.base_gate.label) self.assertEqual("foo", conditonal_controlled_gate.label) - self.assertEqual((clbit, 0), conditonal_controlled_gate.condition) + with self.assertWarns(DeprecationWarning): + self.assertEqual((clbit, 0), conditonal_controlled_gate.condition) def test_inner_outer_label_with_c_if_deepcopy(self): inner_gate = XGate(label="my h gate") @@ -758,13 +767,15 @@ def test_inner_outer_label_with_c_if_deepcopy(self): self.assertTrue(conditonal_controlled_gate.mutable) self.assertEqual("my h gate", conditonal_controlled_gate.base_gate.label) self.assertEqual("foo", conditonal_controlled_gate.label) - self.assertEqual((clbit, 0), conditonal_controlled_gate.condition) + with self.assertWarns(DeprecationWarning): + self.assertEqual((clbit, 0), conditonal_controlled_gate.condition) copied = copy.deepcopy(conditonal_controlled_gate) self.assertIsNot(conditonal_controlled_gate, copied) self.assertTrue(copied.mutable) self.assertEqual("my h gate", copied.base_gate.label) self.assertEqual("foo", copied.label) - self.assertEqual((clbit, 0), copied.condition) + with self.assertWarns(DeprecationWarning): + self.assertEqual((clbit, 0), copied.condition) def test_inner_outer_label_pickle(self): inner_gate = XGate(label="my h gate") diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py index b7bf3365f1d9..ac0ebf1e1faa 100644 --- a/test/python/compiler/test_transpiler.py +++ b/test/python/compiler/test_transpiler.py @@ -2826,7 +2826,8 @@ def test_parallel_singleton_conditional_gate(self, opt_level): [circ, circ], backend, optimization_level=opt_level, seed_transpiler=123456769 ) self.assertTrue(res[0].data[-1].operation.mutable) - self.assertEqual(res[0].data[-1].operation.condition, (res[0].clbits[0], 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(res[0].data[-1].operation.condition, (res[0].clbits[0], 1)) @data(0, 1, 2, 3) def test_backendv2_and_basis_gates(self, opt_level): diff --git a/test/python/converters/test_circuit_to_instruction.py b/test/python/converters/test_circuit_to_instruction.py index 505e3d93bdb7..1b225831bb23 100644 --- a/test/python/converters/test_circuit_to_instruction.py +++ b/test/python/converters/test_circuit_to_instruction.py @@ -72,10 +72,12 @@ def test_flatten_registers_of_circuit_single_bit_cond(self): self.assertEqual(inst.definition[0].qubits, (q[0],)) self.assertEqual(inst.definition[1].qubits, (q[3],)) self.assertEqual(inst.definition[2].qubits, (q[1], q[4])) - - self.assertEqual(inst.definition[0].operation.condition, (c[1], True)) - self.assertEqual(inst.definition[1].operation.condition, (c[3], False)) - self.assertEqual(inst.definition[2].operation.condition, (c[5], True)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(inst.definition[0].operation.condition, (c[1], True)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(inst.definition[1].operation.condition, (c[3], False)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(inst.definition[2].operation.condition, (c[5], True)) def test_flatten_circuit_registerless(self): """Test that the conversion works when the given circuit has bits that are not contained in @@ -212,7 +214,8 @@ def test_registerless_classical_bits(self): test_instruction = test.definition.data[0] expected_instruction = expected.data[0] self.assertIs(type(test_instruction.operation), type(expected_instruction.operation)) - self.assertEqual(test_instruction.operation.condition, (test.definition.clbits[0], 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(test_instruction.operation.condition, (test.definition.clbits[0], 0)) def test_zero_operands(self): """Test that an instruction can be created, even if it has zero operands.""" diff --git a/test/python/dagcircuit/test_collect_blocks.py b/test/python/dagcircuit/test_collect_blocks.py index 4650a7203c22..d8178fdb3a54 100644 --- a/test/python/dagcircuit/test_collect_blocks.py +++ b/test/python/dagcircuit/test_collect_blocks.py @@ -264,11 +264,13 @@ def test_circuit_has_conditional_gates(self): # conditional gate (note that x(1) following the measure is collected into the first # block). block_collector = BlockCollector(circuit_to_dag(qc)) - blocks = block_collector.collect_all_matching_blocks( - lambda node: node.op.name in ["x", "cx"] and not getattr(node.op, "condition", None), - split_blocks=False, - min_block_size=1, - ) + with self.assertWarns(DeprecationWarning): + blocks = block_collector.collect_all_matching_blocks( + lambda node: node.op.name in ["x", "cx"] + and not getattr(node.op, "condition", None), + split_blocks=False, + min_block_size=1, + ) self.assertEqual(len(blocks), 2) self.assertEqual(len(blocks[0]), 4) self.assertEqual(len(blocks[1]), 2) @@ -302,11 +304,13 @@ def test_circuit_has_conditional_gates_dagdependency(self): # conditional gate (note that x(1) following the measure is collected into the first # block). block_collector = BlockCollector(circuit_to_dag(qc)) - blocks = block_collector.collect_all_matching_blocks( - lambda node: node.op.name in ["x", "cx"] and not getattr(node.op, "condition", None), - split_blocks=False, - min_block_size=1, - ) + with self.assertWarns(DeprecationWarning): + blocks = block_collector.collect_all_matching_blocks( + lambda node: node.op.name in ["x", "cx"] + and not getattr(node.op, "condition", None), + split_blocks=False, + min_block_size=1, + ) self.assertEqual(len(blocks), 2) self.assertEqual(len(blocks[0]), 4) self.assertEqual(len(blocks[1]), 2) diff --git a/test/python/dagcircuit/test_dagcircuit.py b/test/python/dagcircuit/test_dagcircuit.py index 10a0ebf06fd6..8fb98577df5b 100644 --- a/test/python/dagcircuit/test_dagcircuit.py +++ b/test/python/dagcircuit/test_dagcircuit.py @@ -118,7 +118,7 @@ def raise_if_dagcircuit_invalid(dag): out_wires = set(dag._out_wires(node._node_id)) node_cond_bits = set( - node.op.condition[0][:] if getattr(node.op, "condition", None) is not None else [] + node.condition[0][:] if getattr(node, "condition", None) is not None else [] ) node_qubits = set(node.qargs) node_clbits = set(node.cargs) @@ -598,7 +598,8 @@ def test_apply_operation_back_conditional(self): self.assertEqual(h_node.qargs, (self.qubit2,)) self.assertEqual(h_node.cargs, ()) - self.assertEqual(h_node.op.condition, h_gate.condition) + with self.assertWarns(DeprecationWarning): + self.assertEqual(h_node.op.condition, h_gate.condition) self.assertEqual( sorted(self.dag._in_edges(h_node._node_id)), @@ -638,7 +639,8 @@ def test_apply_operation_back_conditional_measure(self): self.assertEqual(meas_node.qargs, (self.qubit0,)) self.assertEqual(meas_node.cargs, (self.clbit0,)) - self.assertEqual(meas_node.op.condition, meas_gate.condition) + with self.assertWarns(DeprecationWarning): + self.assertEqual(meas_node.op.condition, meas_gate.condition) self.assertEqual( sorted(self.dag._in_edges(meas_node._node_id)), @@ -683,7 +685,8 @@ def test_apply_operation_back_conditional_measure_to_self(self): self.assertEqual(meas_node.qargs, (self.qubit1,)) self.assertEqual(meas_node.cargs, (self.clbit1,)) - self.assertEqual(meas_node.op.condition, meas_gate.condition) + with self.assertWarns(DeprecationWarning): + self.assertEqual(meas_node.op.condition, meas_gate.condition) self.assertEqual( sorted(self.dag._in_edges(meas_node._node_id)), @@ -2621,7 +2624,8 @@ def test_substitute_with_provided_wire_map_no_propagate_condition(self): sub.h(0) conditioned_cx = CXGate().to_mutable() - conditioned_cx.condition = conditioned_cz.condition + with self.assertWarns(DeprecationWarning): + conditioned_cx.condition = conditioned_cz.condition expected = DAGCircuit() expected.add_qubits(base_qubits) @@ -2719,7 +2723,8 @@ def test_substituting_node_preserves_args_condition(self, inplace): self.assertEqual(replacement_node.op.name, "cz") self.assertEqual(replacement_node.qargs, (qr[1], qr[0])) self.assertEqual(replacement_node.cargs, ()) - self.assertEqual(replacement_node.op.condition, (cr, 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(replacement_node.op.condition, (cr, 1)) self.assertEqual(replacement_node is node_to_be_replaced, inplace) @data(True, False) @@ -3228,7 +3233,8 @@ def test_creg_conditional(self): self.assertEqual(gate_node.op, HGate()) self.assertEqual(gate_node.qargs, (self.qreg[0],)) self.assertEqual(gate_node.cargs, ()) - self.assertEqual(gate_node.op.condition, (self.creg, 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(gate_node.op.condition, (self.creg, 1)) gate_node_preds = list(self.dag.predecessors(gate_node)) gate_node_in_edges = [ @@ -3270,7 +3276,8 @@ def test_clbit_conditional(self): self.assertEqual(gate_node.op, HGate()) self.assertEqual(gate_node.qargs, (self.qreg[0],)) self.assertEqual(gate_node.cargs, ()) - self.assertEqual(gate_node.op.condition, (self.creg[0], 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(gate_node.op.condition, (self.creg[0], 1)) gate_node_preds = list(self.dag.predecessors(gate_node)) gate_node_in_edges = [ diff --git a/test/python/qasm2/test_arxiv_examples.py b/test/python/qasm2/test_arxiv_examples.py index 64df2ac2598b..85ed23e0ed19 100644 --- a/test/python/qasm2/test_arxiv_examples.py +++ b/test/python/qasm2/test_arxiv_examples.py @@ -122,8 +122,7 @@ def test_qft(self, parser): cu1(pi/2) q[3],q[2]; h q[3]; measure q -> c;""" - with self.assertWarns(DeprecationWarning): - parsed = parser(example) + parsed = parser(example) qc = QuantumCircuit(QuantumRegister(4, "q"), ClassicalRegister(4, "c")) qc.x(0) @@ -315,8 +314,7 @@ def test_ripple_carry_adder(self, parser): measure b[2] -> ans[2]; measure b[3] -> ans[3]; measure cout[0] -> ans[4];""" - with self.assertWarns(DeprecationWarning): - parsed = parser(example) + parsed = parser(example) majority_definition = QuantumCircuit([Qubit(), Qubit(), Qubit()]) majority_definition.cx(2, 1) diff --git a/test/python/qasm2/test_structure.py b/test/python/qasm2/test_structure.py index ffdfa907659c..b18ca3aea074 100644 --- a/test/python/qasm2/test_structure.py +++ b/test/python/qasm2/test_structure.py @@ -760,17 +760,20 @@ def test_deepcopy_conditioned_defined_gate(self): my_gate = parsed.data[0].operation self.assertEqual(my_gate.name, "my_gate") - self.assertEqual(my_gate.condition, (parsed.cregs[0], 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(my_gate.condition, (parsed.cregs[0], 1)) copied = copy.deepcopy(parsed) copied_gate = copied.data[0].operation self.assertEqual(copied_gate.name, "my_gate") - self.assertEqual(copied_gate.condition, (copied.cregs[0], 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(copied_gate.condition, (copied.cregs[0], 1)) pickled = pickle.loads(pickle.dumps(parsed)) pickled_gate = pickled.data[0].operation self.assertEqual(pickled_gate.name, "my_gate") - self.assertEqual(pickled_gate.condition, (pickled.cregs[0], 1)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(pickled_gate.condition, (pickled.cregs[0], 1)) class TestOpaque(QiskitTestCase): diff --git a/test/python/transpiler/test_clifford_passes.py b/test/python/transpiler/test_clifford_passes.py index 856287827bd4..2a39c45bc48a 100644 --- a/test/python/transpiler/test_clifford_passes.py +++ b/test/python/transpiler/test_clifford_passes.py @@ -665,7 +665,8 @@ def test_do_not_merge_conditional_gates(self): self.assertEqual(qct.count_ops()["clifford"], 2) # Make sure that the condition on the middle gate is not lost - self.assertIsNotNone(qct.data[1].operation.condition) + with self.assertWarns(DeprecationWarning): + self.assertIsNotNone(qct.data[1].operation.condition) def test_collect_with_cliffords(self): """Make sure that collecting Clifford gates and replacing them by Clifford diff --git a/test/python/transpiler/test_linear_functions_passes.py b/test/python/transpiler/test_linear_functions_passes.py index 85dccbfa53fa..39997ad816ef 100644 --- a/test/python/transpiler/test_linear_functions_passes.py +++ b/test/python/transpiler/test_linear_functions_passes.py @@ -626,7 +626,8 @@ def test_do_not_merge_conditional_gates(self): self.assertEqual(qct.count_ops()["linear_function"], 2) # Make sure that the condition on the middle gate is not lost - self.assertIsNotNone(qct.data[1].operation.condition) + with self.assertWarns(DeprecationWarning): + self.assertIsNotNone(qct.data[1].operation.condition) @combine(do_commutative_analysis=[False, True]) def test_split_layers(self, do_commutative_analysis): diff --git a/test/python/transpiler/test_unroll_3q_or_more.py b/test/python/transpiler/test_unroll_3q_or_more.py index d0388e4fe531..15a90cdf7759 100644 --- a/test/python/transpiler/test_unroll_3q_or_more.py +++ b/test/python/transpiler/test_unroll_3q_or_more.py @@ -71,7 +71,8 @@ def test_decompose_conditional(self): self.assertEqual(len(op_nodes), 15) for node in op_nodes: self.assertIn(node.name, ["h", "t", "tdg", "cx"]) - self.assertEqual(node.op.condition, (cr, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(node.op.condition, (cr, 0)) def test_decompose_unitary(self): """Test unrolling of unitary gate over 4qubits.""" diff --git a/test/utils/base.py b/test/utils/base.py index ce47aa96390a..829ff63c7942 100644 --- a/test/utils/base.py +++ b/test/utils/base.py @@ -180,7 +180,14 @@ def setUpClass(cls): warnings.filterwarnings( "ignore", category=DeprecationWarning, - message=r".*The property.*Instruction\.condition.*is deprecated.*", + message=r".*The property.*condition.*is deprecated.*", + module="qiskit.visualization.circuit", + ) + warnings.filterwarnings( + "ignore", + category=DeprecationWarning, + message=r".*The property.*condition_bits.*is deprecated.*", + module="qiskit.transpiler.passes.scheduling", ) allow_DeprecationWarning_message = [ From 099b6102ba380fb1f05489846ba6eea8a212b96c Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 7 Oct 2024 21:04:32 -0400 Subject: [PATCH 7/9] Add missing test updates --- test/python/circuit/test_compose.py | 27 +++++++++------------------ test/python/circuit/test_singleton.py | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/test/python/circuit/test_compose.py b/test/python/circuit/test_compose.py index a16157ff1ae2..2ecdd2146bfa 100644 --- a/test/python/circuit/test_compose.py +++ b/test/python/circuit/test_compose.py @@ -769,14 +769,10 @@ def test_condition_mapping_ifelseop(self): bit_instruction = test.data[0].operation reg_instruction = test.data[1].operation - with self.assertWarns(DeprecationWarning): - self.assertIs(bit_instruction.condition[0], test_loose) - with self.assertWarns(DeprecationWarning): - self.assertEqual(bit_instruction.condition, (test_loose, True)) - with self.assertWarns(DeprecationWarning): - self.assertIs(reg_instruction.condition[0], test_creg) - with self.assertWarns(DeprecationWarning): - self.assertEqual(reg_instruction.condition, (test_creg, 3)) + 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 @@ -797,14 +793,10 @@ def test_condition_mapping_whileloopop(self): bit_instruction = test.data[0].operation reg_instruction = test.data[1].operation - with self.assertWarns(DeprecationWarning): - self.assertIs(bit_instruction.condition[0], test_loose) - with self.assertWarns(DeprecationWarning): - self.assertEqual(bit_instruction.condition, (test_loose, True)) - with self.assertWarns(DeprecationWarning): - self.assertIs(reg_instruction.condition[0], test_creg) - with self.assertWarns(DeprecationWarning): - self.assertEqual(reg_instruction.condition, (test_creg, 3)) + 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_compose_no_clbits_in_one(self): """Test combining a circuit with cregs to one without""" @@ -872,8 +864,7 @@ def test_expr_condition_is_mapped(self): # Check that the input conditions weren't mutated. for in_condition, instruction in zip((test_1, test_2, test_3), source.data): - with self.assertWarns(DeprecationWarning): - self.assertEqual(in_condition(), instruction.operation.condition) + self.assertEqual(in_condition(), instruction.operation.condition) # Should be `a_dest`, `b_dest` and an added one to account for `c_src`. self.assertEqual(len(dest.cregs), 3) diff --git a/test/python/circuit/test_singleton.py b/test/python/circuit/test_singleton.py index 396fdcc6a240..0274242eec8e 100644 --- a/test/python/circuit/test_singleton.py +++ b/test/python/circuit/test_singleton.py @@ -81,7 +81,8 @@ def test_labeled_condition(self): gate = HGate(label="conditionally special").c_if(clbit, 0) self.assertIsNot(singleton_gate, gate) self.assertEqual(gate.label, "conditionally special") - self.assertEqual(gate.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(gate.condition, (clbit, 0)) def test_default_singleton_copy(self): gate = HGate() @@ -125,7 +126,8 @@ def test_condition_label_copy(self): self.assertIsNot(gate, copied) self.assertEqual(gate, copied) self.assertEqual(copied.label, "conditionally special") - self.assertEqual(copied.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(copied.condition, (clbit, 0)) def test_deepcopy(self): gate = HGate() @@ -154,7 +156,8 @@ def test_condition_label_deepcopy(self): self.assertIsNot(gate, copied) self.assertEqual(gate, copied) self.assertEqual(copied.label, "conditionally special") - self.assertEqual(copied.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(copied.condition, (clbit, 0)) def test_label_deepcopy_new(self): gate = HGate() @@ -206,7 +209,8 @@ def test_round_trip_dag_conversion_with_condition(self): out = dag_to_circuit(dag) self.assertIsNot(qc.data[0].operation, out.data[0].operation) self.assertEqual(qc.data[0].operation, out.data[0].operation) - self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) def test_round_trip_dag_conversion_condition_label(self): qc = QuantumCircuit(1, 1) @@ -217,7 +221,8 @@ def test_round_trip_dag_conversion_condition_label(self): out = dag_to_circuit(dag) self.assertIsNot(qc.data[0].operation, out.data[0].operation) self.assertEqual(qc.data[0].operation, out.data[0].operation) - self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(out.data[0].operation.condition, (qc.cregs[0], 0)) self.assertEqual(out.data[0].operation.label, "conditionally special") def test_condition_via_instructionset(self): @@ -536,7 +541,8 @@ def test_labeled_condition(self): gate = CSwapGate(label="conditionally special").c_if(clbit, 0) self.assertIsNot(singleton_gate, gate) self.assertEqual(gate.label, "conditionally special") - self.assertEqual(gate.condition, (clbit, 0)) + with self.assertWarns(DeprecationWarning): + self.assertEqual(gate.condition, (clbit, 0)) def test_default_singleton_copy(self): gate = CXGate() From 706d24daf8c9da8293d0faf75bbef58261e04433 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 30 Oct 2024 13:17:34 -0400 Subject: [PATCH 8/9] Add filter for aer's condition usage and dag drawer --- test/utils/base.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/utils/base.py b/test/utils/base.py index 829ff63c7942..ffd8a0f10421 100644 --- a/test/utils/base.py +++ b/test/utils/base.py @@ -176,12 +176,19 @@ def setUpClass(cls): module="qiskit.providers.fake_provider.fake_backend", ) + warnings.filterwarnings( + "default", + category=DeprecationWarning, + message=r".*The property.*condition.*is deprecated.*", + module="qiskit_aer", + ) + # Remove with the condition attribute in 2.0: warnings.filterwarnings( "ignore", category=DeprecationWarning, message=r".*The property.*condition.*is deprecated.*", - module="qiskit.visualization.circuit", + module="qiskit.visualization", ) warnings.filterwarnings( "ignore", @@ -196,6 +203,7 @@ def setUpClass(cls): for msg in allow_DeprecationWarning_message: warnings.filterwarnings("default", category=DeprecationWarning, message=msg) + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__setup_called = False From 5a77b3efa1b9e57633b1f0d2eb6e516472d0e091 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 30 Oct 2024 14:06:47 -0400 Subject: [PATCH 9/9] Fix lint --- test/utils/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/utils/base.py b/test/utils/base.py index ffd8a0f10421..133666cfc7ad 100644 --- a/test/utils/base.py +++ b/test/utils/base.py @@ -203,7 +203,6 @@ def setUpClass(cls): for msg in allow_DeprecationWarning_message: warnings.filterwarnings("default", category=DeprecationWarning, message=msg) - def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__setup_called = False