Skip to content

Commit

Permalink
Add JSON serialization support for QasmUGate (#4750)
Browse files Browse the repository at this point in the history
This PR provides the serialization support for the `QasmUGate` class following [serialization.md](https://github.com/quantumlib/Cirq/blob/master/docs/dev/serialization.md). Without this support, `QasmUGate` cannot be properly serialized and deserialized, which causes issue #4728.

The test of serialization ability is done through the .repr and .json files under json_test_data folder. These files are also added for `QasmUGate`.

close #4728
  • Loading branch information
yjt98765 authored Dec 16, 2021
1 parent 645692e commit c33bd9b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
11 changes: 11 additions & 0 deletions cirq-core/cirq/circuits/qasm_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ def _decompose_(self, qubits):
def _value_equality_values_(self):
return self.lmda, self.theta, self.phi

def _json_dict_(self) -> Dict[str, float]:
return {
'theta': self.theta,
'phi': self.phi,
'lmda': self.lmda,
}

@classmethod
def _from_json_dict_(cls, theta: float, phi: float, lmda: float, **kwargs) -> 'QasmUGate':
return cls(theta, phi, lmda)


@value.value_equality
class QasmTwoQubitGate(ops.Gate):
Expand Down
55 changes: 28 additions & 27 deletions cirq-core/cirq/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,35 @@ def _parallel_gate_op(gate, qubits):
import sympy

return {
'AnyIntegerPowerGateFamily': cirq.AnyIntegerPowerGateFamily,
'AmplitudeDampingChannel': cirq.AmplitudeDampingChannel,
'AnyIntegerPowerGateFamily': cirq.AnyIntegerPowerGateFamily,
'AnyUnitaryGateFamily': cirq.AnyUnitaryGateFamily,
'AsymmetricDepolarizingChannel': cirq.AsymmetricDepolarizingChannel,
'BitFlipChannel': cirq.BitFlipChannel,
'BitstringAccumulator': cirq.work.BitstringAccumulator,
'BooleanHamiltonian': cirq.BooleanHamiltonian,
'ProductState': cirq.ProductState,
'CCNotPowGate': cirq.CCNotPowGate,
'CCXPowGate': cirq.CCXPowGate,
'CCZPowGate': cirq.CCZPowGate,
'CNotPowGate': cirq.CNotPowGate,
'Circuit': cirq.Circuit,
'CircuitOperation': cirq.CircuitOperation,
'ClassicallyControlledOperation': cirq.ClassicallyControlledOperation,
'CliffordState': cirq.CliffordState,
'CliffordTableau': cirq.CliffordTableau,
'CNotPowGate': cirq.CNotPowGate,
'ConstantQubitNoiseModel': cirq.ConstantQubitNoiseModel,
'ControlledGate': cirq.ControlledGate,
'ControlledOperation': cirq.ControlledOperation,
'CrossEntropyResult': CrossEntropyResult,
'CrossEntropyResultDict': CrossEntropyResultDict,
'CSwapGate': cirq.CSwapGate,
'CXPowGate': cirq.CXPowGate,
'CZPowGate': cirq.CZPowGate,
'CrossEntropyResult': CrossEntropyResult,
'CrossEntropyResultDict': CrossEntropyResultDict,
'Circuit': cirq.Circuit,
'CircuitOperation': cirq.CircuitOperation,
'CliffordState': cirq.CliffordState,
'CliffordTableau': cirq.CliffordTableau,
'DensePauliString': cirq.DensePauliString,
'DepolarizingChannel': cirq.DepolarizingChannel,
'ConstantQubitNoiseModel': cirq.ConstantQubitNoiseModel,
'Duration': cirq.Duration,
'FrozenCircuit': cirq.FrozenCircuit,
'FSimGate': cirq.FSimGate,
'DensePauliString': cirq.DensePauliString,
'MutableDensePauliString': cirq.MutableDensePauliString,
'MutablePauliString': cirq.MutablePauliString,
'ObservableMeasuredResult': cirq.work.ObservableMeasuredResult,
'GateFamily': cirq.GateFamily,
'GateOperation': cirq.GateOperation,
'Gateset': cirq.Gateset,
Expand All @@ -109,62 +105,67 @@ def _parallel_gate_op(gate, qubits):
'MeasurementGate': cirq.MeasurementGate,
'_MeasurementSpec': cirq.work._MeasurementSpec,
'Moment': cirq.Moment,
'_XEigenState': cirq.value.product_state._XEigenState, # type: ignore
'_YEigenState': cirq.value.product_state._YEigenState, # type: ignore
'_ZEigenState': cirq.value.product_state._ZEigenState, # type: ignore
'MutableDensePauliString': cirq.MutableDensePauliString,
'MutablePauliString': cirq.MutablePauliString,
'_NoNoiseModel': _NoNoiseModel,
'NamedQubit': cirq.NamedQubit,
'NamedQid': cirq.NamedQid,
'NoIdentifierQubit': cirq.testing.NoIdentifierQubit,
'ObservableMeasuredResult': cirq.work.ObservableMeasuredResult,
'OpIdentifier': cirq.OpIdentifier,
'_PauliX': cirq.ops.pauli_gates._PauliX,
'_PauliY': cirq.ops.pauli_gates._PauliY,
'_PauliZ': cirq.ops.pauli_gates._PauliZ,
'ParamResolver': cirq.ParamResolver,
'ParallelGate': cirq.ParallelGate,
'ParallelGateFamily': cirq.ParallelGateFamily,
'PauliMeasurementGate': cirq.PauliMeasurementGate,
'PauliString': cirq.PauliString,
'_PauliX': cirq.ops.pauli_gates._PauliX,
'_PauliY': cirq.ops.pauli_gates._PauliY,
'_PauliZ': cirq.ops.pauli_gates._PauliZ,
'PhaseDampingChannel': cirq.PhaseDampingChannel,
'PhaseFlipChannel': cirq.PhaseFlipChannel,
'PhaseGradientGate': cirq.PhaseGradientGate,
'PhasedFSimGate': cirq.PhasedFSimGate,
'PhasedISwapPowGate': cirq.PhasedISwapPowGate,
'PhasedXPowGate': cirq.PhasedXPowGate,
'PhasedXZGate': cirq.PhasedXZGate,
'StatePreparationChannel': cirq.StatePreparationChannel,
'ProductState': cirq.ProductState,
'ProjectorString': cirq.ProjectorString,
'ProjectorSum': cirq.ProjectorSum,
'RandomGateChannel': cirq.RandomGateChannel,
'QasmUGate': cirq.circuits.qasm_output.QasmUGate,
'_QubitAsQid': raw_types._QubitAsQid,
'QuantumFourierTransformGate': cirq.QuantumFourierTransformGate,
'RandomGateChannel': cirq.RandomGateChannel,
'RepetitionsStoppingCriteria': cirq.work.RepetitionsStoppingCriteria,
'ResetChannel': cirq.ResetChannel,
'Result': cirq.Result,
'Rx': cirq.Rx,
'Ry': cirq.Ry,
'Rz': cirq.Rz,
'SingleQubitCliffordGate': cirq.SingleQubitCliffordGate,
'SingleQubitPauliStringGateOperation': cirq.SingleQubitPauliStringGateOperation,
'SingleQubitReadoutCalibrationResult': cirq.experiments.SingleQubitReadoutCalibrationResult,
'StabilizerStateChForm': cirq.StabilizerStateChForm,
'StatePreparationChannel': cirq.StatePreparationChannel,
'SwapPowGate': cirq.SwapPowGate,
'SymmetricalQidPair': cirq.SymmetricalQidPair,
'TaggedOperation': cirq.TaggedOperation,
'TiltedSquareLattice': cirq.TiltedSquareLattice,
'TrialResult': cirq.Result, # keep support for Cirq < 0.11.
'Result': cirq.Result,
'Rx': cirq.Rx,
'Ry': cirq.Ry,
'Rz': cirq.Rz,
'_UnconstrainedDevice': cirq.devices.unconstrained_device._UnconstrainedDevice,
'VarianceStoppingCriteria': cirq.work.VarianceStoppingCriteria,
'VirtualTag': cirq.VirtualTag,
'WaitGate': cirq.WaitGate,
'_QubitAsQid': raw_types._QubitAsQid,
# The formatter keeps putting this back
# pylint: disable=line-too-long
'XEBPhasedFSimCharacterizationOptions': cirq.experiments.XEBPhasedFSimCharacterizationOptions,
# pylint: enable=line-too-long
'_XEigenState': cirq.value.product_state._XEigenState, # type: ignore
'XPowGate': cirq.XPowGate,
'XXPowGate': cirq.XXPowGate,
'_YEigenState': cirq.value.product_state._YEigenState, # type: ignore
'YPowGate': cirq.YPowGate,
'YYPowGate': cirq.YYPowGate,
'_ZEigenState': cirq.value.product_state._ZEigenState, # type: ignore
'ZPowGate': cirq.ZPowGate,
'ZZPowGate': cirq.ZZPowGate,
# Old types, only supported for backwards-compatibility
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/protocols/json_test_data/QasmUGate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cirq_type": "QasmUGate",
"theta": 0.1,
"phi": 0.2,
"lmda": 0.3
}
1 change: 1 addition & 0 deletions cirq-core/cirq/protocols/json_test_data/QasmUGate.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq.circuits.qasm_output.QasmUGate(0.1, 0.2, 0.3)

0 comments on commit c33bd9b

Please sign in to comment.