Skip to content

Commit

Permalink
Formatting and import fix (#62)
Browse files Browse the repository at this point in the history
* black and import fix

* merge conflics
  • Loading branch information
dberthault authored Sep 9, 2024
1 parent addd76c commit ef3a94d
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 50 deletions.
12 changes: 6 additions & 6 deletions qoqo_qiskit/src/qoqo_qiskit/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ def __init__(
self.compilation = compilation

# Internal _run_circuit method
def _run_circuit(
self, circuit: Circuit
) -> Tuple[Job, str, RegistersWithLengths,]:
def _run_circuit(self, circuit: Circuit) -> Tuple[
Job,
str,
RegistersWithLengths,
]:
if not circuit.__class__.__name__ == "Circuit":
raise TypeError("The input is not a valid Qoqo Circuit instance.")

Expand Down Expand Up @@ -436,9 +438,7 @@ def run_measurement(
output_complex_register_dict,
)

def run_program(
self, program: QuantumProgram, params_values: List[List[float]]
) -> Optional[
def run_program(self, program: QuantumProgram, params_values: List[List[float]]) -> Optional[
List[
Union[
Tuple[
Expand Down
5 changes: 4 additions & 1 deletion qoqo_qiskit/src/qoqo_qiskit/backend/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def _transform_job_result_single(


def _transform_job_result_list(
memory: bool, sim_type: str, result: Result, output_registers: List[RegistersWithLengths]
memory: bool,
sim_type: str,
result: Result,
output_registers: List[RegistersWithLengths],
) -> None:
if sim_type == "automatic":
res_list = result.get_memory() if memory else result.get_counts()
Expand Down
5 changes: 5 additions & 0 deletions qoqo_qiskit/src/qoqo_qiskit/transpiler_helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
# or implied. See the License for the specific language governing permissions and limitations under
# the License.
"""Helper to use qiskit transpiler for qoqo circuits."""

from qoqo_qiskit.transpiler_helper.transpiler_helper import (
transpile_with_qiskit,
transpile_program_with_qiskit,
)
43 changes: 32 additions & 11 deletions qoqo_qiskit/tests/backend/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ def test_run_circuit_list_errors(operations: List[Any]) -> None:

with pytest.raises(ValueError) as exc:
_ = backend.run_circuit_list([circuit_0, circuit_1])
assert "The input is a list of Qoqo Circuits with different simulation types." in str(
exc.value
assert (
"The input is a list of Qoqo Circuits with different simulation types."
in str(exc.value)
)

circuit_2 = Circuit()
Expand All @@ -174,7 +175,10 @@ def test_run_circuit_list_errors(operations: List[Any]) -> None:

with pytest.raises(ValueError) as exc:
_ = backend.run_circuit_list([circuit_2, circuit_3])
assert "The input is a list of Qoqo Circuits with different number of shots." in str(exc.value)
assert (
"The input is a list of Qoqo Circuits with different number of shots."
in str(exc.value)
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -416,9 +420,13 @@ def test_measurement(operations: List[Any]) -> None:
circuit += ops.DefinitionBit("ri", len(involved_qubits), True)
circuit += ops.PragmaRepeatedMeasurement("ri", 10)

pzpinput = PauliZProductInput(number_qubits=len(involved_qubits), use_flipped_measurement=True)
pzpinput = PauliZProductInput(
number_qubits=len(involved_qubits), use_flipped_measurement=True
)

measurement = PauliZProduct(constant_circuit=None, circuits=[circuit], input=pzpinput)
measurement = PauliZProduct(
constant_circuit=None, circuits=[circuit], input=pzpinput
)

try:
_ = backend.run_measurement(measurement=measurement)
Expand Down Expand Up @@ -576,7 +584,9 @@ def test_overwrite() -> None:
circuit_2 += ops.MeasureQubit(0, "same", 0)
circuit_2 += ops.PragmaSetNumberOfMeasurements(2, "same")

measurement = ClassicalRegister(constant_circuit=None, circuits=[circuit_1, circuit_2])
measurement = ClassicalRegister(
constant_circuit=None, circuits=[circuit_1, circuit_2]
)

try:
output = backend.run_measurement_registers(measurement=measurement)
Expand Down Expand Up @@ -650,9 +660,13 @@ def test_run_program() -> None:
init_circuit += ops.DefinitionBit("ro", 1, True)
init_circuit += ops.PragmaRepeatedMeasurement("ro", 1000, None)

measurement = ClassicalRegister(constant_circuit=None, circuits=[init_circuit, init_circuit])
measurement = ClassicalRegister(
constant_circuit=None, circuits=[init_circuit, init_circuit]
)

program = QuantumProgram(measurement=measurement, input_parameter_names=["angle_0", "angle_1"])
program = QuantumProgram(
measurement=measurement, input_parameter_names=["angle_0", "angle_1"]
)

res = backend.run_program(
program=program, params_values=[[0.785, 0.238], [0.234, 0.653], [0.875, 0.612]]
Expand Down Expand Up @@ -736,7 +750,9 @@ def test_run_measurement_queued(memory: bool) -> None:
circuit_1 += ops.DefinitionBit("ri", 2, True)
circuit_1 += ops.PragmaRepeatedMeasurement("ri", 50)

measurement = ClassicalRegister(constant_circuit=None, circuits=[circuit_0, circuit_1])
measurement = ClassicalRegister(
constant_circuit=None, circuits=[circuit_0, circuit_1]
)

qpr: QueuedProgramRun = backend.run_measurement_queued(measurement=measurement)

Expand Down Expand Up @@ -785,11 +801,16 @@ def test_run_program_queued() -> None:
input=measurement_input,
)

program = QuantumProgram(measurement=measurement, input_parameter_names=["angle_0", "angle_1"])
program = QuantumProgram(
measurement=measurement, input_parameter_names=["angle_0", "angle_1"]
)

with pytest.raises(ValueError) as exc:
_ = backend.run_program_queued(program=program, params_values=[[0.4]])
assert "Wrong number of parameters 2 parameters expected 1 parameters given." in str(exc.value)
assert (
"Wrong number of parameters 2 parameters expected 1 parameters given."
in str(exc.value)
)

with pytest.raises(ValueError) as exc:
_ = backend.run_program_queued(program=program, params_values=[])
Expand Down
8 changes: 6 additions & 2 deletions qoqo_qiskit/tests/backend/test_queued_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def test_poll_result() -> None:
registers_info=run_1[2],
)

measurement = ClassicalRegister(constant_circuit=None, circuits=[run_0[3], run_1[3]])
measurement = ClassicalRegister(
constant_circuit=None, circuits=[run_0[3], run_1[3]]
)
qpr = QueuedProgramRun(
measurement=measurement,
queued_circuits=[qcr_0, qcr_1],
Expand Down Expand Up @@ -174,7 +176,9 @@ def test_overwrite() -> None:
registers_info=run_1[2],
)

measurement = ClassicalRegister(constant_circuit=None, circuits=[run_0[3], run_1[3]])
measurement = ClassicalRegister(
constant_circuit=None, circuits=[run_0[3], run_1[3]]
)
qpr = QueuedProgramRun(
measurement=measurement,
queued_circuits=[qcr_0, qcr_1],
Expand Down
71 changes: 54 additions & 17 deletions qoqo_qiskit/tests/transpiler_helper/test_transpiler_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def test_basic_circuit_basic_gates() -> None:
circuit_res += ops.SqrtPauliX(0)
circuit_res += ops.SqrtPauliX(0)

transpiled_circuit = transpile_with_qiskit(circuit, [{"basis_gates": ["sx", "rz", "cz"]}])
transpiled_circuit = transpile_with_qiskit(
circuit, [{"basis_gates": ["sx", "rz", "cz"]}]
)
assert transpiled_circuit == circuit_res


Expand All @@ -57,7 +59,9 @@ def test_medium_circuit_basic_gates() -> None:
circuit_res += ops.SqrtPauliX(1)
circuit_res += ops.RotateZ(1, 1.5707963267948966)

transpiled_circuit = transpile_with_qiskit(circuit, [{"basis_gates": ["sx", "rz", "cz"]}])
transpiled_circuit = transpile_with_qiskit(
circuit, [{"basis_gates": ["sx", "rz", "cz"]}]
)

assert transpiled_circuit == circuit_res

Expand Down Expand Up @@ -163,14 +167,19 @@ def test_multiple_circuits_backend() -> None:
circuit_res_2 += ops.RotateZ(1, 1.5707963267948966)

backend = FakeManilaV2()
transpiled_circuits = transpile_with_qiskit([circuit_1, circuit_2], [{"backend": backend}])
transpiled_circuits = transpile_with_qiskit(
[circuit_1, circuit_2], [{"backend": backend}]
)

transpiled_circuit_dag = CircuitDag()
circuit_res_dag = CircuitDag()
transpiled_circuit_dag = transpiled_circuit_dag.from_circuit(transpiled_circuits[1])
circuit_res_dag = circuit_res_dag.from_circuit(circuit_res_2)

assert transpiled_circuits[0] == circuit_res_1 and transpiled_circuit_dag == circuit_res_dag
assert (
transpiled_circuits[0] == circuit_res_1
and transpiled_circuit_dag == circuit_res_dag
)


def assert_quantum_program_equal(
Expand All @@ -185,9 +194,15 @@ def assert_quantum_program_equal(
Raises:
AssertionError: if the quantum programs are not equal
"""
assert quantum_program_1.input_parameter_names() == quantum_program2.input_parameter_names()
assert (
quantum_program_1.input_parameter_names()
== quantum_program2.input_parameter_names()
)
if not isinstance(quantum_program_1.measurement(), ClassicalRegister):
assert quantum_program_1.measurement().input() == quantum_program2.measurement().input()
assert (
quantum_program_1.measurement().input()
== quantum_program2.measurement().input()
)
assert (
quantum_program_1.measurement().constant_circuit()
== quantum_program2.measurement().constant_circuit()
Expand Down Expand Up @@ -225,12 +240,18 @@ def test_basic_program_basic_gates() -> None:
circuit_res_2 += ops.SqrtPauliX(1)
circuit_res_2 += ops.RotateZ(1, 1.5707963267948966)

measurement = ClassicalRegister(constant_circuit=None, circuits=[circuit_1, circuit_2])
measurement = ClassicalRegister(
constant_circuit=None, circuits=[circuit_1, circuit_2]
)
measurement_res = ClassicalRegister(
constant_circuit=None, circuits=[circuit_res_1, circuit_res_2]
)
quantum_program = QuantumProgram(measurement=measurement, input_parameter_names=["x"])
quantum_program_res = QuantumProgram(measurement=measurement_res, input_parameter_names=["x"])
quantum_program = QuantumProgram(
measurement=measurement, input_parameter_names=["x"]
)
quantum_program_res = QuantumProgram(
measurement=measurement_res, input_parameter_names=["x"]
)

transpiled_program = transpile_program_with_qiskit(
quantum_program, [{"basis_gates": ["sx", "rz", "cz"]}]
Expand Down Expand Up @@ -280,8 +301,12 @@ def test_program_with_constant_circuit_basic_gates() -> None:
circuits=[circuit_res_1, circuit_res_2],
input=measurement_input,
)
quantum_program = QuantumProgram(measurement=measurement, input_parameter_names=["x"])
quantum_program_res = QuantumProgram(measurement=measurement_res, input_parameter_names=["x"])
quantum_program = QuantumProgram(
measurement=measurement, input_parameter_names=["x"]
)
quantum_program_res = QuantumProgram(
measurement=measurement_res, input_parameter_names=["x"]
)

transpiled_program = transpile_program_with_qiskit(
quantum_program, [{"basis_gates": ["sx", "rz", "cz"]}]
Expand Down Expand Up @@ -335,11 +360,17 @@ def test_quantum_program_backend() -> None:
circuits=[circuit_res_1, circuit_res_2, circuit_res_3],
input=measurement_input,
)
quantum_program = QuantumProgram(measurement=measurement, input_parameter_names=["x"])
quantum_program_res = QuantumProgram(measurement=measurement_res, input_parameter_names=["x"])
quantum_program = QuantumProgram(
measurement=measurement, input_parameter_names=["x"]
)
quantum_program_res = QuantumProgram(
measurement=measurement_res, input_parameter_names=["x"]
)

backend = FakeManilaV2()
transpiled_program = transpile_program_with_qiskit(quantum_program, [{"backend": backend}])
transpiled_program = transpile_program_with_qiskit(
quantum_program, [{"backend": backend}]
)

assert_quantum_program_equal(transpiled_program, quantum_program_res)

Expand Down Expand Up @@ -402,10 +433,16 @@ def test_quantum_program_with_constant_circuit_backend() -> None:
circuits=[circuit_res_1, circuit_res_2, circuit_res_3],
input=measurement_input,
)
quantum_program = QuantumProgram(measurement=measurement, input_parameter_names=["x"])
quantum_program_res = QuantumProgram(measurement=measurement_res, input_parameter_names=["x"])
quantum_program = QuantumProgram(
measurement=measurement, input_parameter_names=["x"]
)
quantum_program_res = QuantumProgram(
measurement=measurement_res, input_parameter_names=["x"]
)

backend = FakeManilaV2()
transpiled_program = transpile_program_with_qiskit(quantum_program, [{"backend": backend}])
transpiled_program = transpile_program_with_qiskit(
quantum_program, [{"backend": backend}]
)

assert_quantum_program_equal(transpiled_program, quantum_program_res)
4 changes: 3 additions & 1 deletion qoqo_qiskit_devices/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [(master_doc, "qoqoqiskitdevices", "qoqo_qiskit_devices Documentation", [author], 1)]
man_pages = [
(master_doc, "qoqoqiskitdevices", "qoqo_qiskit_devices Documentation", [author], 1)
]


# -- Options for Texinfo output -------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions qoqo_qiskit_devices/python_tests/test_initialization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test qoqo_qiskit_devices initialization"""

# Copyright © 2023 HQS Quantum Simulations GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
Expand Down
Loading

0 comments on commit ef3a94d

Please sign in to comment.