Skip to content

Commit

Permalink
Overwrite Bugfix (#52)
Browse files Browse the repository at this point in the history
* update removed

* tests

* versioning
  • Loading branch information
mlodi-hqs authored Apr 22, 2024
1 parent d26e0d7 commit 5c74288
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This changelog tracks changes of the qoqo_qiskit project starting at version 0.1.0 (initial release).

### 0.4.1

* Fixed overwriting registers bug

### 0.4.0

* Added `run_program()`, `run_program_queued()` allowing for multiple runs in one call thanks to a list of lists of parameter values
Expand Down
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion qoqo_qiskit/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "qoqo_qiskit"
version = "0.4.0"
version = "0.4.1"
license = { file = "LICENSE" }
authors = [
{ name = "HQS Quantum Simulation GmbH", email = "[email protected]" },
Expand Down
18 changes: 15 additions & 3 deletions qoqo_qiskit/src/qoqo_qiskit/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,21 @@ def run_measurement_registers(
tmp_complex_register_dict,
) = self.run_circuit(run_circuit)

output_bit_register_dict.update(tmp_bit_register_dict)
output_float_register_dict.update(tmp_float_register_dict)
output_complex_register_dict.update(tmp_complex_register_dict)
for key, value_bools in tmp_bit_register_dict.items():
if key in output_bit_register_dict:
output_bit_register_dict[key].extend(value_bools)
else:
output_bit_register_dict[key] = value_bools
for key, value_floats in tmp_float_register_dict.items():
if key in output_float_register_dict:
output_float_register_dict[key].extend(value_floats)
else:
output_float_register_dict[key] = value_floats
for key, value_complexes in tmp_complex_register_dict.items():
if key in output_complex_register_dict:
output_complex_register_dict[key].extend(value_complexes)
else:
output_complex_register_dict[key] = value_complexes

return (
output_bit_register_dict,
Expand Down
54 changes: 45 additions & 9 deletions qoqo_qiskit/src/qoqo_qiskit/backend/queued_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,21 @@ def __init__(self, measurement: Any, queued_circuits: List[QueuedCircuitRun]) ->
] = ({}, {}, {})
for circuit in self._queued_circuits:
if circuit._qoqo_result is not None:
self._registers[0].update(circuit._qoqo_result[0])
self._registers[1].update(circuit._qoqo_result[1])
self._registers[2].update(circuit._qoqo_result[2])
for key, value_bools in circuit._qoqo_result[0].items():
if key in self._registers[0]:
self._registers[0][key].extend(value_bools)
else:
self._registers[0][key] = value_bools
for key, value_floats in circuit._qoqo_result[1].items():
if key in self._registers[1]:
self._registers[1][key].extend(value_floats)
else:
self._registers[1][key] = value_floats
for key, value_complexes in circuit._qoqo_result[2].items():
if key in self._registers[2]:
self._registers[2][key].extend(value_complexes)
else:
self._registers[2][key] = value_complexes

def to_json(self) -> str:
"""Convert self to a JSON string.
Expand Down Expand Up @@ -250,9 +262,21 @@ def from_json(string: str) -> QueuedProgramRun:
circ_instance = QueuedCircuitRun.from_json(circuit)
queued_circuits_deserialised.append(circ_instance)
if circ_instance._qoqo_result is not None:
registers[0].update(circ_instance._qoqo_result[0])
registers[1].update(circ_instance._qoqo_result[1])
registers[2].update(circ_instance._qoqo_result[2])
for key, value_bools in circ_instance._qoqo_result[0].items():
if key in registers[0]:
registers[0][key].extend(value_bools)
else:
registers[0][key] = value_bools
for key, value_floats in circ_instance._qoqo_result[1].items():
if key in registers[1]:
registers[1][key].extend(value_floats)
else:
registers[1][key] = value_floats
for key, value_complexes in circ_instance._qoqo_result[2].items():
if key in registers[2]:
registers[2][key].extend(value_complexes)
else:
registers[2][key] = value_complexes

if json_dict["measurement_type"] == "PauliZProduct":
measurement = measurements.PauliZProduct.from_json(json_dict["measurement"])
Expand Down Expand Up @@ -294,9 +318,21 @@ def poll_result(
for i, queued_circuit in enumerate(self._queued_circuits):
res = queued_circuit.poll_result()
if res is not None:
self._registers[0].update(res[0]) # add results to bit registers
self._registers[1].update(res[1]) # add results to float registers
self._registers[2].update(res[2]) # add results to complex registers
for key, value_bools in res[0].items():
if key in self._registers[0]:
self._registers[0][key].extend(value_bools)
else:
self._registers[0][key] = value_bools
for key, value_floats in res[1].items():
if key in self._registers[1]:
self._registers[1][key].extend(value_floats)
else:
self._registers[1][key] = value_floats
for key, value_complexes in res[2].items():
if key in self._registers[2]:
self._registers[2][key].extend(value_complexes)
else:
self._registers[2][key] = value_complexes
all_finished[i] = True

if not all(all_finished):
Expand Down
34 changes: 34 additions & 0 deletions qoqo_qiskit/tests/backend/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,40 @@ def test_split() -> None:

assert _split(shot_result_ws, clas_regs) == _split(shot_result_no_ws, clas_regs)

def test_overwrite() -> None:
"""Tests overwriting registers."""
backend = QoqoQiskitBackend()

circuit_1 = Circuit()
circuit_1 += ops.DefinitionBit("same", 1, True)
circuit_1 += ops.PauliX(0)
circuit_1 += ops.MeasureQubit(0, "same", 0)
circuit_1 += ops.PragmaSetNumberOfMeasurements(2, "same")

circuit_2 = Circuit()
circuit_2 += ops.DefinitionBit("same", 1, True)
circuit_2 += ops.PauliX(0)
circuit_2 += ops.PauliX(0)
circuit_2 += ops.MeasureQubit(0, "same", 0)
circuit_2 += ops.PragmaSetNumberOfMeasurements(2, "same")


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

try:
output = backend.run_measurement_registers(measurement=measurement)
except Exception:
assert False

# output should look like ({'same': [[True], [True], [False], [False]]}, {}, {})
assert len(output[0]["same"]) == 4
assert output[0]["same"][0][0]
assert output[0]["same"][1][0]
assert not output[0]["same"][2][0]
assert not output[0]["same"][3][0]
assert not output[1]
assert not output[2]

def test_run_program() -> None:
"""Test QoqoQiskitBackend.run_program method."""
backend = QoqoQiskitBackend()
Expand Down
48 changes: 48 additions & 0 deletions qoqo_qiskit/tests/backend/test_queued_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,54 @@ def test_poll_result() -> None:
assert res_qpr["ri"]


def test_overwrite() -> None:
"""Test overwriting registers."""
run_0 = _mocked_run(memory="True")
run_1 = _mocked_run()

qcr_0 = QueuedCircuitRun(
job=run_0[0],
memory=True,
sim_type=run_0[1],
registers_info=run_0[2],
)
qcr_1 = QueuedCircuitRun(
job=run_1[0],
memory=False,
sim_type=run_1[1],
registers_info=run_1[2],
)

measurement = ClassicalRegister(constant_circuit=None, circuits=[run_0[3], run_1[3]])
qpr = QueuedProgramRun(
measurement=measurement,
queued_circuits=[qcr_0, qcr_1],
)

# Making sure that the simulations are finished
time.sleep(1)

res_qpr, _, _ = qpr.poll_result()

assert len(res_qpr["ri"]) == 20

run_2 = _mocked_run()
qcr_2 = QueuedCircuitRun(
job=run_2[0],
memory=False,
sim_type=run_2[1],
registers_info=run_2[2],
)
qcr_2.poll_result()

qpr = QueuedProgramRun(
measurement=measurement,
queued_circuits=[qcr_0, qcr_1, qcr_2],
)

assert len(qpr._registers[0]["ri"]) == 30


# For pytest
if __name__ == "__main__":
pytest.main(sys.argv)
2 changes: 1 addition & 1 deletion qoqo_qiskit_devices/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qoqo_qiskit_devices"
version = "0.4.0"
version = "0.4.1"
authors = ["HQS Quantum Simulations <[email protected]>"]
license = "Apache-2.0"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion qoqo_qiskit_devices/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "qoqo_qiskit_devices"
version = "0.4.0"
version = "0.4.1"
dependencies = [
'qoqo_calculator_pyo3>=1.1',
'qoqo>=1.9',
Expand Down
Loading

0 comments on commit 5c74288

Please sign in to comment.