Skip to content

Commit

Permalink
Rename some test helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Dumazert authored and jrj-d committed Nov 7, 2023
1 parent ba42bcb commit bcbdd1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
14 changes: 8 additions & 6 deletions tests/local/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
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)
params = initializes[0].operation.params
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:
Expand All @@ -47,26 +49,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:
Expand Down
32 changes: 17 additions & 15 deletions tests/plugins/test_state_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,40 @@
)


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)
params = initializes[0].operation.params
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()])

# int(2) should map to '10'
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)
Expand All @@ -42,30 +54,20 @@ 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()])

# Initialize('10') is broken down into '0' on qubit 0, '1' on qubit 1
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)
Expand Down

0 comments on commit bcbdd1c

Please sign in to comment.