diff --git a/tests/local/test_backend.py b/tests/local/test_backend.py index 0c54061..9943bd6 100644 --- a/tests/local/test_backend.py +++ b/tests/local/test_backend.py @@ -12,7 +12,7 @@ from .processor_fixture import SimpleAllToAllProcessor, SimpleProcessor -def _check_initialize(circuit: QuantumCircuit, expected: str) -> None: +def _assert_one_initialize(circuit: QuantumCircuit, expected: str) -> None: initializes = circuit.get_instructions('initialize') assert len(initializes) == 1 assert isinstance(initializes[0].operation, Initialize) @@ -20,7 +20,7 @@ def _check_initialize(circuit: QuantumCircuit, expected: str) -> None: assert params == list(expected) -def _check_initializes(circuit: QuantumCircuit, expected: List[str]) -> None: +def _assert_many_initializes(circuit: QuantumCircuit, expected: List[str]) -> None: initializes = circuit.get_instructions('initialize') assert len(initializes) == len(expected) for initialize in initializes: @@ -47,26 +47,26 @@ def test_translation_plugin() -> None: circ = QuantumCircuit(1) circ.initialize('+') transpiled = transpile(circ, backend) - _check_initialize(transpiled, '+') + _assert_one_initialize(transpiled, '+') # if no reset / initialize, add Initialize('0') circ = QuantumCircuit(1) circ.x(0) transpiled = transpile(circ, backend) - _check_initialize(transpiled, '0') + _assert_one_initialize(transpiled, '0') # if reset, convert to Intialize('0') circ = QuantumCircuit(1) circ.reset(0) transpiled = transpile(circ, backend) - _check_initialize(transpiled, '0') + _assert_one_initialize(transpiled, '0') # if reset, convert to Intialize('0') circ = QuantumCircuit(3) circ.initialize(2, [0, 1]) circ.reset(2) transpiled = transpile(circ, backend) - _check_initializes(transpiled, ['0', '1', '0']) + _assert_many_initializes(transpiled, ['0', '1', '0']) def test_synthesize_rz() -> None: diff --git a/tests/plugins/test_state_preparation.py b/tests/plugins/test_state_preparation.py index 5f913ac..4c4afe3 100644 --- a/tests/plugins/test_state_preparation.py +++ b/tests/plugins/test_state_preparation.py @@ -12,7 +12,7 @@ ) -def _check_initialize(circuit: QuantumCircuit, expected: str) -> None: +def _assert_one_initialize(circuit: QuantumCircuit, expected: str) -> None: initializes = circuit.get_instructions('initialize') assert len(initializes) == 1 assert isinstance(initializes[0].operation, Initialize) @@ -20,6 +20,16 @@ def _check_initialize(circuit: QuantumCircuit, expected: str) -> None: assert params == list(expected) +def _assert_many_initializes(circuit: QuantumCircuit, expected: List[str]) -> None: + initializes = circuit.get_instructions('initialize') + assert len(initializes) == len(expected) + for initialize in initializes: + assert len(initialize.qubits) == 1 + qubit = circuit.find_bit(initialize.qubits[0]).index + assert isinstance(initialize.operation, Initialize) + assert initialize.operation.params[0] == expected[qubit] + + def test_int_to_label() -> None: pm = PassManager([IntToLabelInitializePass()]) @@ -27,13 +37,13 @@ def test_int_to_label() -> None: circ = QuantumCircuit(2) circ.initialize(2) transpiled = pm.run(circ) - _check_initialize(transpiled, '10') + _assert_one_initialize(transpiled, '10') # a string label should pass unchanged circ = QuantumCircuit(2) circ.initialize('+0') transpiled = pm.run(circ) - _check_initialize(transpiled, '+0') + _assert_one_initialize(transpiled, '+0') # a state vector should fail circ = QuantumCircuit(2) @@ -42,16 +52,6 @@ def test_int_to_label() -> None: pm.run(circ) -def _check_initializes(circuit: QuantumCircuit, expected: List[str]) -> None: - initializes = circuit.get_instructions('initialize') - assert len(initializes) == len(expected) - for initialize in initializes: - assert len(initialize.qubits) == 1 - qubit = circuit.find_bit(initialize.qubits[0]).index - assert isinstance(initialize.operation, Initialize) - assert initialize.operation.params[0] == expected[qubit] - - def test_break_down() -> None: pm = PassManager([BreakDownInitializePass()]) @@ -59,13 +59,13 @@ def test_break_down() -> None: circ = QuantumCircuit(2) circ.initialize('10') transpiled = pm.run(circ) - _check_initializes(transpiled, ['0', '1']) + _assert_many_initializes(transpiled, ['0', '1']) # Initialize('+') is broken down into '1' on qubit 0 circ = QuantumCircuit(2) circ.initialize('+', 0) transpiled = pm.run(circ) - _check_initializes(transpiled, ['+']) + _assert_many_initializes(transpiled, ['+']) # fails on int circ = QuantumCircuit(2)