Skip to content

Commit

Permalink
Add check for serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
fdmalone committed Jul 1, 2024
1 parent 0c1a3fa commit 4371589
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
16 changes: 10 additions & 6 deletions recirq/qcqmc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

from cirq.protocols.json_serialization import DEFAULT_RESOLVERS, ObjectFactory

from .blueprint import (BlueprintData, BlueprintParamsRobustShadow,
BlueprintParamsTrialWf)
from .blueprint import (
BlueprintData,
BlueprintParamsRobustShadow,
BlueprintParamsTrialWf,
)
from .experiment import ExperimentData, SimulatedExperimentParams
from .fermion_mode import FermionicMode
from .hamiltonian import (HamiltonianData, HamiltonianFileParams,
PyscfHamiltonianParams)
from .hamiltonian import HamiltonianData, HamiltonianFileParams, PyscfHamiltonianParams
from .layer_spec import LayerSpec
from .trial_wf import (PerfectPairingPlusTrialWavefunctionParams,
TrialWavefunctionData)
from .trial_wf import PerfectPairingPlusTrialWavefunctionParams, TrialWavefunctionData


@lru_cache()
Expand All @@ -50,6 +52,8 @@ def _resolve_json(cirq_type: str) -> Optional[ObjectFactory]:
BlueprintParamsTrialWf,
BlueprintParamsRobustShadow,
BlueprintData,
ExperimentData,
SimulatedExperimentParams,
]
}.get(cirq_type, None)

Expand Down
15 changes: 6 additions & 9 deletions recirq/qcqmc/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,12 @@ class SimulatedExperimentParams(data.Params):
blueprint_params: blueprint.BlueprintParams
n_samples_per_clifford: int
noise_model_name: str
noise_model_params: Optional[Tuple[float, ...]] = attrs.field(converter=_to_tuple)
noise_model_params: Optional[Tuple[float, ...]] = attrs.field(
converter=_to_tuple, default=None
)
seed: int = 0
path_prefix: str = ""

def __post_init__(self):
"""A little special sauce to make sure that these end up as a tuple."""
if self.noise_model_params is not None:
object.__setattr__(
self, "noise_model_params", tuple(self.noise_model_params)
)

@property
def path_string(self) -> str:
return (
Expand Down Expand Up @@ -91,7 +86,9 @@ class ExperimentData(data.Data):
metadata: Dict[str, Any] = attrs.field(factory=dict)

def _json_dict_(self):
return attrs.asdict(self)
simple_dict = attrs.asdict(self)
simple_dict["params"] = self.params._json_dict_() # type: ignore
return simple_dict


def build_experiment(
Expand Down
6 changes: 6 additions & 0 deletions recirq/qcqmc/experiment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from typing import Tuple

import cirq

from recirq.qcqmc.blueprint import BlueprintData
from recirq.qcqmc.experiment import SimulatedExperimentParams, build_experiment
from recirq.qcqmc.hamiltonian import HamiltonianData
Expand Down Expand Up @@ -44,3 +46,7 @@ def test_small_experiment_raw_samples_shape(
raw_samples = experiment.raw_samples

assert raw_samples.shape == (17, 31, 4)

exp2 = cirq.read_json(json_text=cirq.to_json(experiment))
print(exp2)
assert exp2 == experiment

0 comments on commit 4371589

Please sign in to comment.