-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QCQMC Part 7: Add Experiment classes #354
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
3e38a11
Add some tests for unit test.
fdmalone bcb3d4f
Tidy up.
fdmalone aa622a5
Converters.
fdmalone 9285e4d
Test slow test solution.
fdmalone b5d88a0
Add tests for qubit_maps.
fdmalone a50a58f
Update tests.
fdmalone 9a29bc2
Add trial tests.
fdmalone b60599e
Add generators test.
fdmalone caa0046
Add missing changes.
fdmalone 4330f42
Remove debug code.
fdmalone 7817f73
Remove prints.
fdmalone 64017f8
Remove redundant test docstrings.
fdmalone 58fadff
Add blueprint.
fdmalone 08dd528
Skip slow test.
fdmalone 7ddae81
Move conftest and skip another slow test.
fdmalone 33ed0cd
Fix serialization.
fdmalone 7227dfc
Fix _json_dict_.
fdmalone 2585997
Merge branch 'master' into qcqmc-6
fdmalone d66c1e1
Remove comparator.
fdmalone fa71795
Fix merge errors.
fdmalone 30ea62b
Address review comments.
fdmalone 9de66cb
Add experiment class.
fdmalone 42262bc
Fix fixture.
fdmalone 863a4e3
Add check for serialization.
fdmalone 094df6b
Add experiment.
fdmalone 467a458
Address comments.
fdmalone f1b8d6a
Merge branch 'master' into qcqmc-7
fdmalone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
# Copyright 2024 Google | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from datetime import datetime | ||
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union | ||
|
||
import attrs | ||
import cirq | ||
import numpy as np | ||
import qsimcirq | ||
from pytz import timezone | ||
|
||
from recirq.qcqmc import blueprint, config, data, for_refactor | ||
|
||
|
||
@attrs.frozen(repr=False) | ||
class SimulatedExperimentParams(data.Params): | ||
"""Class for storing the parameters that specify an ExperimentData object. | ||
|
||
This stage of the experiment concerns itself with executing circuits and doing | ||
classical post-processing of shadow tomography data. | ||
|
||
Args: | ||
name: A `Params` name for this experiment. | ||
blueprint_params: Backreference to the `BlueprintParams` preceding this stage. | ||
n_samples_per_clifford: Number of circuit repetitions to take for each clifford. | ||
noise_model_name: For simulation; see `utilities.get_noise_model`. | ||
noise_model_params: For simulation; see `utilities.get_noise_model`. | ||
seed: The random seed to use for simulation. | ||
path_prefix: An optional path string prefix for the output files. | ||
""" | ||
|
||
name: str | ||
blueprint_params: blueprint.BlueprintParams | ||
n_samples_per_clifford: int | ||
noise_model_name: str | ||
noise_model_params: Optional[Tuple[float, ...]] = attrs.field( | ||
converter=lambda x: tuple(x) if x is not None else None, default=None | ||
) | ||
seed: int = 0 | ||
path_prefix: str = "" | ||
|
||
@property | ||
def path_string(self) -> str: | ||
return ( | ||
self.path_prefix + config.OUTDIRS.DEFAULT_EXPERIMENT_DIRECTORY + self.name | ||
) | ||
|
||
def _json_dict_(self): | ||
simple_dict = attrs.asdict(self) | ||
simple_dict["blueprint_params"] = self.blueprint_params | ||
return simple_dict | ||
|
||
|
||
@attrs.frozen(repr=False, eq=False) | ||
class ExperimentData(data.Data): | ||
"""The data defining the experimental result. | ||
|
||
Args: | ||
params: The experimental parameters. | ||
raw_samples: An array of shape [n_cliffords, n_samples_per_clifford, n_qubits] | ||
containing the bitstrings sampled from each of the different | ||
circuits. | ||
metadata: Any metadata associated with the run. | ||
""" | ||
|
||
params: SimulatedExperimentParams | ||
raw_samples: np.ndarray = attrs.field(converter=np.asarray) | ||
metadata: Dict[str, Any] = attrs.field(factory=dict) | ||
|
||
def _json_dict_(self): | ||
simple_dict = attrs.asdict(self) | ||
simple_dict["params"] = self.params | ||
return simple_dict | ||
|
||
|
||
@classmethod | ||
def build_experiment_from_dependencies( | ||
cls, params: SimulatedExperimentParams, *, dependencies: Dict[data.Params, data.Data] | ||
) -> 'ExperimentData': | ||
"""Builds an ExperimentData from ExperimentParams and any dependencies. | ||
|
||
Args: | ||
params: The experimental parameters. | ||
dependencies: The dependencies leading up to this point (in particular the blueprint.) | ||
""" | ||
bp = dependencies[params.blueprint_params] | ||
assert isinstance(bp, blueprint.BlueprintData) | ||
assert params.blueprint_params == bp.params | ||
|
||
noise_model = for_refactor.get_noise_model( | ||
params.noise_model_name, params.noise_model_params | ||
) | ||
|
||
raw_samples = get_samples_from_simulation( | ||
bp.compiled_circuit, | ||
bp.resolvers, | ||
noise_model, | ||
params.n_samples_per_clifford, | ||
params.seed, | ||
) | ||
|
||
metadata = get_experimental_metadata() | ||
|
||
return ExperimentData(params=params, raw_samples=raw_samples, metadata=metadata) | ||
|
||
|
||
def get_samples_from_simulation( | ||
circuit: cirq.Circuit, | ||
resolvers: List[cirq.ParamResolverOrSimilarType], | ||
noise_model: Union[None, cirq.NoiseModel], | ||
n_samples_per_clifford: int, | ||
seed: Optional[int] = None, | ||
simulate_single_precision: bool = config.SINGLE_PRECISION_DEFAULT, | ||
) -> np.ndarray: | ||
"""Samples the circuits and returns an array of sampled bits. | ||
|
||
Args: | ||
circuits: The shadow tomography circuits. | ||
resolvers: A list of cirq parameter resolvers. | ||
noise_model: An optional cirq.NoiseModel for the simulated experiment. | ||
n_samples_per_clifford: The number of samples to take per clifford sample. | ||
seed: An optional random seed | ||
simulate_single_precision: Use single precision instead of double | ||
precision for the circuit simulation. | ||
|
||
Returns: | ||
raw_samples: An array of shape [n_cliffords, n_samples_per_clifford, | ||
n_qubits] containing the bitstrings sampled from each of the different | ||
circuits. | ||
|
||
""" | ||
simulator = qsimcirq.QSimSimulator() | ||
|
||
sampled_bitstrings = [] | ||
|
||
if noise_model is not None: | ||
circuit = cirq.Circuit( | ||
noise_model.noisy_moments(circuit, sorted(circuit.all_qubits())) | ||
) | ||
|
||
for _, resolver in enumerate(resolvers): | ||
results = simulator.run( | ||
circuit, repetitions=n_samples_per_clifford, param_resolver=resolver | ||
) | ||
|
||
outcomes = results.measurements["all"] | ||
sampled_bitstrings.append(outcomes) | ||
|
||
raw_samples = np.stack(sampled_bitstrings) | ||
return raw_samples | ||
|
||
|
||
def get_experimental_metadata() -> Dict[str, object]: | ||
"""Gets some metadata to store along with the results for an experiment. | ||
|
||
Returns: | ||
A dictionary of useful metadata including the date and time of the experiment. | ||
""" | ||
|
||
date_time = datetime.now() | ||
pacific_tz = timezone("US/Pacific") | ||
pacific_date_time = date_time.astimezone(pacific_tz) | ||
|
||
formatted_date_time = pacific_date_time.strftime("%m/%d/%Y, %H:%M:%S") | ||
|
||
metadata: Dict[str, Any] = {} | ||
metadata["PST_formatted_date_time"] = formatted_date_time | ||
metadata["iso_formatted_date_time"] = date_time.isoformat() | ||
|
||
return metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2024 Google | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Tuple | ||
|
||
import cirq | ||
|
||
from recirq.qcqmc.blueprint import BlueprintData | ||
from recirq.qcqmc.experiment import SimulatedExperimentParams, get_experimental_metadata, ExperimentData | ||
from recirq.qcqmc.hamiltonian import HamiltonianData | ||
from recirq.qcqmc.trial_wf import TrialWavefunctionData | ||
|
||
|
||
def test_small_experiment_raw_samples_shape( | ||
fixture_4_qubit_ham_trial_wf_and_blueprint: Tuple[ | ||
HamiltonianData, TrialWavefunctionData, BlueprintData | ||
] | ||
): | ||
_, _, blueprint_data = fixture_4_qubit_ham_trial_wf_and_blueprint | ||
|
||
simulated_experiment_params = SimulatedExperimentParams( | ||
name="test_1", | ||
blueprint_params=blueprint_data.params, | ||
noise_model_name="None", | ||
noise_model_params=(0,), | ||
n_samples_per_clifford=31, | ||
seed=1, | ||
) | ||
|
||
experiment = ExperimentData.build_experiment_from_dependencies( | ||
params=simulated_experiment_params, | ||
dependencies={blueprint_data.params: blueprint_data}, | ||
) | ||
|
||
raw_samples = experiment.raw_samples | ||
|
||
assert raw_samples.shape == (17, 31, 4) | ||
|
||
exp2 = cirq.read_json(json_text=cirq.to_json(experiment)) | ||
assert exp2 == experiment | ||
|
||
def test_get_experimental_metadata(): | ||
md = get_experimental_metadata() | ||
assert md.get('PST_formatted_date_time') is not None | ||
assert md.get('iso_formatted_date_time') is not None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test file seems short. Does it test everything in experiments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does at least call every function (after a small update) but the test coverage is not great. I'd need @wjhuggins to help writing some sensible physics tests of the shadow tomography protocol but I think that can be a follow up (we discussed writing some better documentation of the shadow tomography part of the code which is probably of most interest to QIS people).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, yeah, let's make it a follow up PR.