Skip to content

Commit 4741aa9

Browse files
committed
Remove [De]SerializingArg, GateOp[De]Serializer
1 parent 54d460d commit 4741aa9

File tree

7 files changed

+5
-1235
lines changed

7 files changed

+5
-1235
lines changed

cirq-google/cirq_google/__init__.py

-4
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,8 @@
111111
CIRCUIT_SERIALIZER,
112112
CircuitSerializer,
113113
CircuitOpDeserializer,
114-
DeserializingArg,
115-
GateOpDeserializer,
116114
CircuitOpSerializer,
117-
GateOpSerializer,
118115
Serializer,
119-
SerializingArg,
120116
)
121117

122118
from cirq_google.workflow import (

cirq-google/cirq_google/json_test_data/spec.py

-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,17 @@
2626
'ConvertToSqrtIswapGates',
2727
'ConvertToSycamoreGates',
2828
'ConvertToXmonGates',
29-
'DeserializingArg',
3029
'Engine',
3130
'EngineJob',
3231
'EngineProcessor',
3332
'EngineProgram',
3433
'FSimPhaseCorrections',
3534
'NoiseModelFromGoogleNoiseProperties',
3635
'ProtoVersion',
37-
'GateOpSerializer',
38-
'GateOpDeserializer',
3936
'GreedySequenceSearchStrategy',
4037
'PhasedFSimCalibrationError',
4138
'PhasedFSimEngineSimulator',
4239
'PerQubitDepolarizingWithDampedReadoutNoiseModel',
43-
'SerializingArg',
4440
'THETA_ZETA_GAMMA_FLOQUET_PHASED_FSIM_CHARACTERIZATION',
4541
'ProcessorSampler',
4642
'ValidatingSampler',

cirq-google/cirq_google/serialization/__init__.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,10 @@
1616

1717
from cirq_google.serialization.arg_func_langs import arg_from_proto
1818

19-
2019
from cirq_google.serialization.circuit_serializer import CircuitSerializer, CIRCUIT_SERIALIZER
2120

22-
from cirq_google.serialization.op_deserializer import (
23-
CircuitOpDeserializer,
24-
DeserializingArg,
25-
GateOpDeserializer,
26-
)
21+
from cirq_google.serialization.op_deserializer import CircuitOpDeserializer
2722

28-
from cirq_google.serialization.op_serializer import (
29-
CircuitOpSerializer,
30-
GateOpSerializer,
31-
SerializingArg,
32-
)
23+
from cirq_google.serialization.op_serializer import CircuitOpSerializer
3324

3425
from cirq_google.serialization.serializer import Serializer

cirq-google/cirq_google/serialization/op_deserializer.py

+1-215
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, Callable, Dict, List, Optional, Sequence
16-
from dataclasses import dataclass
15+
from typing import Any, List
1716

1817
import abc
1918
import sympy
2019

2120
import cirq
2221
from cirq_google.api import v2
23-
from cirq_google.ops.calibration_tag import CalibrationTag
2422
from cirq_google.serialization import arg_func_langs
2523

2624

@@ -65,218 +63,6 @@ def from_proto(
6563
"""
6664

6765

68-
@dataclass(frozen=True)
69-
class _DeserializingArg:
70-
"""HACK: non-deprecated version of DeserializingArg.
71-
72-
This is used by global gatesets to bypass the behavior that deprecation warnings thrown
73-
during module loading fail unit tests.
74-
"""
75-
76-
serialized_name: str
77-
constructor_arg_name: str
78-
value_func: Optional[Callable[[arg_func_langs.ARG_LIKE], Any]] = None
79-
required: bool = True
80-
default: Any = None
81-
82-
83-
@cirq._compat.deprecated_class(
84-
deadline='v0.16',
85-
fix='Will no longer be used because GateOpDeserializer is deprecated.'
86-
' CircuitSerializer will be the only supported circuit serializer going forward.',
87-
)
88-
@dataclass(frozen=True)
89-
class DeserializingArg(_DeserializingArg):
90-
"""Specification of the arguments to deserialize an argument to a gate.
91-
92-
Args:
93-
serialized_name: The serialized name of the gate that is being
94-
deserialized.
95-
constructor_arg_name: The name of the argument in the constructor of
96-
the gate corresponding to this serialized argument.
97-
value_func: Sometimes a value from the serialized proto needs to
98-
converted to an appropriate type or form. This function takes the
99-
serialized value and returns the appropriate type. Defaults to
100-
None.
101-
required: Whether a value must be specified when constructing the
102-
deserialized gate. Defaults to True.
103-
default: default value to set if the value is not present in the
104-
arg. If set, required is ignored.
105-
"""
106-
107-
108-
class _GateOpDeserializer(OpDeserializer):
109-
"""HACK: non-deprecated version of GateOpDeserializer.
110-
111-
This is used by global gatesets to bypass the behavior that deprecation warnings thrown
112-
during module loading fail unit tests.
113-
"""
114-
115-
def __init__(
116-
self,
117-
serialized_gate_id: str,
118-
gate_constructor: Callable,
119-
args: Sequence[DeserializingArg],
120-
num_qubits_param: Optional[str] = None,
121-
op_wrapper: Callable[
122-
[cirq.Operation, v2.program_pb2.Operation], cirq.Operation
123-
] = lambda x, y: x,
124-
deserialize_tokens: Optional[bool] = True,
125-
):
126-
self._serialized_gate_id = serialized_gate_id
127-
self._gate_constructor = gate_constructor
128-
self._args = args
129-
self._num_qubits_param = num_qubits_param
130-
self._op_wrapper = op_wrapper
131-
self._deserialize_tokens = deserialize_tokens
132-
133-
@property
134-
def serialized_id(self):
135-
return self._serialized_gate_id
136-
137-
def from_proto(
138-
self,
139-
proto: v2.program_pb2.Operation,
140-
*,
141-
arg_function_language: str = '',
142-
constants: List[v2.program_pb2.Constant] = None,
143-
deserialized_constants: List[Any] = None, # unused
144-
) -> cirq.Operation:
145-
qubits = [v2.qubit_from_proto_id(q.id) for q in proto.qubits]
146-
args = self._args_from_proto(proto, arg_function_language=arg_function_language)
147-
if self._num_qubits_param is not None:
148-
args[self._num_qubits_param] = len(qubits)
149-
gate = self._gate_constructor(**args)
150-
op = self._op_wrapper(gate.on(*qubits), proto)
151-
if self._deserialize_tokens:
152-
which = proto.WhichOneof('token')
153-
if which == 'token_constant_index':
154-
if not constants:
155-
raise ValueError(
156-
'Proto has references to constants table '
157-
'but none was passed in, value ='
158-
f'{proto}'
159-
)
160-
op = op.with_tags(
161-
CalibrationTag(constants[proto.token_constant_index].string_value)
162-
)
163-
elif which == 'token_value':
164-
op = op.with_tags(CalibrationTag(proto.token_value))
165-
return op
166-
167-
def _args_from_proto(
168-
self, proto: v2.program_pb2.Operation, *, arg_function_language: str
169-
) -> Dict[str, arg_func_langs.ARG_LIKE]:
170-
return_args = {}
171-
for arg in self._args:
172-
if arg.serialized_name not in proto.args:
173-
if arg.default:
174-
return_args[arg.constructor_arg_name] = arg.default
175-
continue
176-
elif arg.required:
177-
raise ValueError(
178-
f'Argument {arg.serialized_name} '
179-
'not in deserializing args, but is required.'
180-
)
181-
182-
value = arg_func_langs.arg_from_proto(
183-
proto.args[arg.serialized_name],
184-
arg_function_language=arg_function_language,
185-
required_arg_name=None if not arg.required else arg.serialized_name,
186-
)
187-
188-
if arg.value_func is not None and value is not None:
189-
value = arg.value_func(value)
190-
191-
if value is not None:
192-
return_args[arg.constructor_arg_name] = value
193-
return return_args
194-
195-
196-
@cirq._compat.deprecated_class(
197-
deadline='v0.16',
198-
fix='Will no longer be supported.'
199-
' CircuitSerializer will be the only supported circuit serializer going forward.',
200-
)
201-
class GateOpDeserializer(_GateOpDeserializer):
202-
"""Describes how to deserialize a proto to a given Gate type.
203-
204-
Attributes:
205-
serialized_gate_id: The id used when serializing the gate.
206-
"""
207-
208-
def __init__(
209-
self,
210-
serialized_gate_id: str,
211-
gate_constructor: Callable,
212-
args: Sequence[DeserializingArg],
213-
num_qubits_param: Optional[str] = None,
214-
op_wrapper: Callable[
215-
[cirq.Operation, v2.program_pb2.Operation], cirq.Operation
216-
] = lambda x, y: x,
217-
deserialize_tokens: Optional[bool] = True,
218-
):
219-
"""Constructs a deserializer.
220-
221-
Args:
222-
serialized_gate_id: The serialized id of the gate that is being
223-
deserialized.
224-
gate_constructor: A function that produces the deserialized gate
225-
given arguments from args.
226-
args: A list of the arguments to be read from the serialized
227-
gate and the information required to use this to construct
228-
the gate using the gate_constructor above.
229-
num_qubits_param: Some gate constructors require that the number
230-
of qubits be passed to their constructor. This is the name
231-
of the parameter in the constructor for this value. If None,
232-
no number of qubits is passed to the constructor.
233-
op_wrapper: An optional Callable to modify the resulting
234-
GateOperation, for instance, to add tags
235-
deserialize_tokens: Whether to convert tokens to
236-
CalibrationTags. Defaults to True.
237-
"""
238-
super().__init__(
239-
serialized_gate_id,
240-
gate_constructor,
241-
args,
242-
num_qubits_param,
243-
op_wrapper,
244-
deserialize_tokens,
245-
)
246-
247-
def from_proto(
248-
self,
249-
proto: v2.program_pb2.Operation,
250-
*,
251-
arg_function_language: str = '',
252-
constants: List[v2.program_pb2.Constant] = None,
253-
deserialized_constants: List[Any] = None, # unused
254-
) -> cirq.Operation:
255-
"""Turns a cirq_google.api.v2.Operation proto into a GateOperation.
256-
257-
Args:
258-
proto: The proto object to be deserialized.
259-
arg_function_language: The `arg_function_language` field from
260-
`Program.Language`.
261-
constants: The list of Constant protos referenced by constant
262-
table indices in `proto`.
263-
deserialized_constants: Unused in this method.
264-
265-
Returns:
266-
The deserialized GateOperation represented by `proto`.
267-
268-
Raises:
269-
ValueError: If the proto references a missing constants table, or a required arg is
270-
missing.
271-
"""
272-
return super().from_proto(
273-
proto,
274-
arg_function_language=arg_function_language,
275-
constants=constants,
276-
deserialized_constants=deserialized_constants,
277-
)
278-
279-
28066
class CircuitOpDeserializer(OpDeserializer):
28167
"""Describes how to serialize CircuitOperations."""
28268

0 commit comments

Comments
 (0)