Skip to content

Commit

Permalink
Replace AbstractEngineProcessorShim (quantumlib#4842)
Browse files Browse the repository at this point in the history
We were using `AbstractEngineProcessorShim` pending quantumlib#4644 and friends. Those PRs have landed. For data hygiene as part of the workflow tools, we need to record what processor was used. This PR introduces an abstract `ProcessorRecord` interface and three implementations for mapping (processor_id, whether it's a simulator, what sort of noise) tuples to an AbstractProcessor instance for doing the work.

 - Remove `AbstractEngineProcessorShim`
 - Add dataclasses for data hygene
 - Now we can have proper serialization tests for anything that saves a `QuantumRuntimeConfiguration`, hence the new json files
 - We don't need to shim over `cirq.read_json` in tests that make use of `AbstractEngineProcessorShim` any more
 - Side effect: the new engine-processor-backed instances mean we have to use "real" qubits in the tests, so there are some knock-on test changes.

cc @dstrain115 @MichaelBroughton
  • Loading branch information
mpharrigan authored and 95-martin-orion committed Mar 2, 2022
1 parent a5b3a5a commit c4122ef
Show file tree
Hide file tree
Showing 22 changed files with 539 additions and 107 deletions.
4 changes: 4 additions & 0 deletions cirq-google/cirq_google/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@
CouldNotPlaceError,
NaiveQubitPlacer,
RandomDevicePlacer,
ProcessorRecord,
EngineProcessorRecord,
SimulatedProcessorRecord,
SimulatedProcessorWithLocalDeviceRecord,
)

from cirq_google import experimental
Expand Down
5 changes: 5 additions & 0 deletions cirq-google/cirq_google/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:
'cirq.google.QuantumRuntimeConfiguration': cirq_google.QuantumRuntimeConfiguration,
'cirq.google.NaiveQubitPlacer': cirq_google.NaiveQubitPlacer,
'cirq.google.RandomDevicePlacer': cirq_google.RandomDevicePlacer,
'cirq.google.EngineProcessorRecord': cirq_google.EngineProcessorRecord,
'cirq.google.SimulatedProcessorRecord': cirq_google.SimulatedProcessorRecord,
# pylint: disable=line-too-long
'cirq.google.SimulatedProcessorWithLocalDeviceRecord': cirq_google.SimulatedProcessorWithLocalDeviceRecord,
# pylint: enable=line-too-long
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cirq_type": "cirq.google.EngineProcessorRecord",
"processor_id": "rainbow"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq_google.EngineProcessorRecord(processor_id='rainbow')
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"cirq_type": "cirq.google.ExecutableGroupResult",
"runtime_configuration": {
"cirq_type": "cirq.google.QuantumRuntimeConfiguration",
"processor_record": {
"cirq_type": "cirq.google.SimulatedProcessorRecord",
"processor_id": "rainbow",
"noise_strength": 0
},
"run_id": "unit-test",
"random_seed": null,
"qubit_placer": {
"cirq_type": "cirq.google.NaiveQubitPlacer"
}
},
"shared_runtime_info": {
"cirq_type": "cirq.google.SharedRuntimeInfo",
"run_id": "my run"
},
"executable_results": [
{
"cirq_type": "cirq.google.ExecutableResult",
"spec": {
"cirq_type": "cirq.google.KeyValueExecutableSpec",
"executable_family": "cirq_google.algo_benchmarks.example",
"key_value_pairs": [
[
"name",
"test-spec-0"
]
]
},
"runtime_info": {
"cirq_type": "cirq.google.RuntimeInfo",
"execution_index": 0,
"qubit_placement": null
},
"raw_data": {
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": []
},
"measurements": {
"z": {
"packed_digits": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"binary": true,
"dtype": "float64",
"shape": [
1000,
4
]
}
}
}
},
{
"cirq_type": "cirq.google.ExecutableResult",
"spec": {
"cirq_type": "cirq.google.KeyValueExecutableSpec",
"executable_family": "cirq_google.algo_benchmarks.example",
"key_value_pairs": [
[
"name",
"test-spec-1"
]
]
},
"runtime_info": {
"cirq_type": "cirq.google.RuntimeInfo",
"execution_index": 1,
"qubit_placement": null
},
"raw_data": {
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": []
},
"measurements": {
"z": {
"packed_digits": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"binary": true,
"dtype": "float64",
"shape": [
1000,
4
]
}
}
}
},
{
"cirq_type": "cirq.google.ExecutableResult",
"spec": {
"cirq_type": "cirq.google.KeyValueExecutableSpec",
"executable_family": "cirq_google.algo_benchmarks.example",
"key_value_pairs": [
[
"name",
"test-spec-2"
]
]
},
"runtime_info": {
"cirq_type": "cirq.google.RuntimeInfo",
"execution_index": 2,
"qubit_placement": null
},
"raw_data": {
"cirq_type": "ResultDict",
"params": {
"cirq_type": "ParamResolver",
"param_dict": []
},
"measurements": {
"z": {
"packed_digits": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"binary": true,
"dtype": "float64",
"shape": [
1000,
4
]
}
}
}
}
]
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"cirq_type": "cirq.google.QuantumRuntimeConfiguration",
"processor_record": {
"cirq_type": "cirq.google.SimulatedProcessorRecord",
"processor_id": "rainbow",
"noise_strength": 0
},
"run_id": "unit-test",
"random_seed": null,
"qubit_placer": {
"cirq_type": "cirq.google.NaiveQubitPlacer"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq_google.QuantumRuntimeConfiguration(processor_record=cirq_google.SimulatedProcessorRecord(processor_id='rainbow', noise_strength=0), run_id='unit-test', random_seed=None, qubit_placer=cirq_google.NaiveQubitPlacer())
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cirq_type": "cirq.google.SimulatedProcessorRecord",
"processor_id": "rainbow",
"noise_strength": 0.001
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq_google.SimulatedProcessorRecord(processor_id='rainbow', noise_strength=0.001)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cirq_type": "cirq.google.SimulatedProcessorWithLocalDeviceRecord",
"processor_id": "rainbow",
"noise_strength": 0.001
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq_google.SimulatedProcessorWithLocalDeviceRecord(processor_id='rainbow', noise_strength=0.001)
11 changes: 3 additions & 8 deletions cirq-google/cirq_google/json_test_data/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,11 @@
'ExecutableGroupResultFilesystemRecord',
'NaiveQubitPlacer',
'RandomDevicePlacer',
'EngineProcessorRecord',
'SimulatedProcessorRecord',
'SimulatedProcessorWithLocalDeviceRecord',
]
},
tested_elsewhere=[
# Until `AbstractEngineProcessor` is implemented, we are using
# `AbstractEngineProcessorShim` and a mocked implementation for the `processor` argument
# in tests for `QuantumRuntimeConfiguration` (which is copied into `ExecutableGroupResult`).
# Therefore, we test json roundtrippability for these two classes in quantum_runtime_test.py
'cirq.google.QuantumRuntimeConfiguration',
'cirq.google.ExecutableGroupResult',
],
resolver_cache=_class_resolver_dictionary(),
deprecated={
'_NamedConstantXmonDevice': 'v0.15',
Expand Down
7 changes: 7 additions & 0 deletions cirq-google/cirq_google/workflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@
NaiveQubitPlacer,
RandomDevicePlacer,
)

from cirq_google.workflow.processor_record import (
ProcessorRecord,
EngineProcessorRecord,
SimulatedProcessorRecord,
SimulatedProcessorWithLocalDeviceRecord,
)

This file was deleted.

13 changes: 4 additions & 9 deletions cirq-google/cirq_google/workflow/io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
import cirq_google as cg
import numpy as np
from cirq_google.workflow.io import _FilesystemSaver
from cirq_google.workflow.quantum_runtime_test import (
_MockEngineProcessor,
patch_cirq_default_resolvers,
)

assert patch_cirq_default_resolvers, 'Pytest fixture -- not unused import'


def cg_assert_equivalent_repr(value):
Expand Down Expand Up @@ -90,12 +84,13 @@ def test_egr_filesystem_record_from_json(tmpdir):
)


def test_filesystem_saver(tmpdir, patch_cirq_default_resolvers):
assert patch_cirq_default_resolvers
def test_filesystem_saver(tmpdir):
run_id = 'asdf'
fs_saver = _FilesystemSaver(base_data_dir=tmpdir, run_id=run_id)

rt_config = cg.QuantumRuntimeConfiguration(processor=_MockEngineProcessor(), run_id=run_id)
rt_config = cg.QuantumRuntimeConfiguration(
processor_record=cg.SimulatedProcessorWithLocalDeviceRecord('rainbow'), run_id=run_id
)
shared_rt_info = cg.SharedRuntimeInfo(run_id=run_id)
fs_saver.initialize(rt_config, shared_rt_info=shared_rt_info)

Expand Down
Loading

0 comments on commit c4122ef

Please sign in to comment.