See Stable
See Nightly
A class for serializing and deserializing programs and operations.
Inherits From: Serializer
cirq_google.serialization.SerializableGateSet(
gate_set_name: str,
serializers: Iterable[cirq_google.serialization.op_serializer.OpSerializer
],
deserializers: Iterable[cirq_google.serialization.op_deserializer.OpDeserializer
]
)
This class is for cirq_google.api.v2 protos.
Args
gate_set_name
The name used to identify the gate set.
serializers
The OpSerializers to use for serialization.
Multiple serializers for a given gate type are allowed and
will be checked for a given type in the order specified here.
This allows for a given gate type to be serialized into
different serialized form depending on the parameters of the
gate.
deserializers
The OpDeserializers to convert serialized
forms of gates or circuits into Operations.
Attributes
name
The name of the serializer.
Methods
deserialize
View source
deserialize(
proto: cirq_google.api.v2.program_pb2.Program
,
device: Optional[cirq.Device] = None
) -> cirq.Circuit
Deserialize a Circuit from a cirq_google.api.v2.Program.
Args
proto
A dictionary representing a cirq_google.api.v2.Program proto.
device
If the proto is for a schedule, a device is required
Otherwise optional.
Returns
The deserialized Circuit, with a device if device was
not None.
Raises
ValueError
If the given proto has no language specified or it mismatched the
name specified for this serializable gate set. In addition if the program
is a schedule and no device was specified.
NotImplementedError
If the program does not contain a circuit or schedule.
deserialize_circuit_op
View source
deserialize_circuit_op(
operation_proto: cirq_google.api.v2.program_pb2.CircuitOperation
,
*,
arg_function_language: str = '',
constants: Optional[List[v2.program_pb2.Constant]] = None,
deserialized_constants: Optional[List[Any]] = None
) -> cirq.CircuitOperation
Deserialize a CircuitOperation from a cirq.google.api.v2.CircuitOperation.
Args
operation_proto
A dictionary representing a
cirq.google.api.v2.CircuitOperation proto.
arg_function_language
The arg_function_language
field from
Program.Language
.
constants
The list of Constant protos referenced by constant
table indices in proto
.
deserialized_constants
The deserialized contents of constants
.
Returns
The deserialized CircuitOperation.
Raises
ValueError
If the deserializer does not support deserializing CircuitOperation
.
deserialize_gate_op
View source
deserialize_gate_op(
operation_proto: cirq_google.api.v2.program_pb2.Operation
,
*,
arg_function_language: str = '',
constants: Optional[List[v2.program_pb2.Constant]] = None,
deserialized_constants: Optional[List[Any]] = None
) -> cirq.Operation
Deserialize an Operation from a cirq_google.api.v2.Operation.
Args
operation_proto
A dictionary representing a
cirq.google.api.v2.Operation proto.
arg_function_language
The arg_function_language
field from
Program.Language
.
constants
The list of Constant protos referenced by constant
table indices in proto
.
deserialized_constants
The deserialized contents of constants
.
cirq_google.api.v2.Operation proto.
Returns
The deserialized Operation.
Raises
ValueError
If the gate cannot be deserialized.
deserialize_op
View source
deserialize_op(
operation_proto: Union[cirq_google.api.v2.program_pb2.Operation
, cirq_google.api.v2.program_pb2.CircuitOperation
],
**kwargs
) -> cirq.Operation
Disambiguation for operation deserialization.
is_supported
View source
is_supported(
op_tree: cirq.OP_TREE
) -> bool
Whether the given object contains only supported operations.
is_supported_operation
View source
is_supported_operation(
op: cirq.Operation
) -> bool
Whether or not the given gate can be serialized by this gate set.
serialize
View source
serialize(
program: cirq.AbstractCircuit,
msg: Optional[cirq_google.api.v2.program_pb2.Program
] = None,
*,
arg_function_language: Optional[str] = None,
use_constants: Optional[bool] = True
) -> cirq_google.api.v2.program_pb2.Program
Serialize a Circuit to cirq_google.api.v2.Program proto.
Args
program
The Circuit to serialize.
msg
An optional proto object to populate with the serialization
results.
arg_function_language
The arg_function_language
field from
Program.Language
.
use_constants
Whether to use constants in serialization. This is
required to be True for serializing CircuitOperations.
Raises
NotImplementedError
If the program type is not supported.
serialize_circuit_op
View source
serialize_circuit_op(
op: cirq.Operation,
msg: Optional[cirq_google.api.v2.program_pb2.CircuitOperation
] = None,
*,
arg_function_language: Optional[str] = '',
constants: Optional[List[v2.program_pb2.Constant]] = None,
raw_constants: Optional[Dict[Any, int]] = None
) -> Union[cirq_google.api.v2.program_pb2.Operation
, cirq_google.api.v2.program_pb2.CircuitOperation
]
Serialize a CircuitOperation to cirq.google.api.v2.CircuitOperation proto.
Args
op
The circuit operation to serialize.
msg
An optional proto object to populate with the serialization
results.
arg_function_language
The arg_function_language
field from
Program.Language
.
constants
The list of previously-serialized Constant protos.
raw_constants
A map raw objects to their respective indices in
constants
.
Returns
The cirq.google.api.v2.CircuitOperation proto.
Raises
ValueError
If one of constants
or raw_constants
is None, or the circuit operation
cannot be serialized.
serialize_gate_op
View source
serialize_gate_op(
op: cirq.Operation,
msg: Optional[cirq_google.api.v2.program_pb2.Operation
] = None,
*,
arg_function_language: Optional[str] = '',
constants: Optional[List[v2.program_pb2.Constant]] = None,
raw_constants: Optional[Dict[Any, int]] = None
) -> cirq_google.api.v2.program_pb2.Operation
Serialize an Operation to cirq_google.api.v2.Operation proto.
Args
op
The operation to serialize.
msg
An optional proto object to populate with the serialization
results.
arg_function_language
The arg_function_language
field from
Program.Language
.
constants
The list of previously-serialized Constant protos.
raw_constants
A map raw objects to their respective indices in
constants
.
Returns
The cirq.google.api.v2.Operation proto.
Raises
ValueError
If the op cannot be serialized.
serialize_op
View source
serialize_op(
op: cirq.Operation,
msg: Union[None, cirq_google.api.v2.program_pb2.Operation
, cirq_google.api.v2.program_pb2.CircuitOperation
] = None,
**kwargs
) -> Union[cirq_google.api.v2.program_pb2.Operation
, cirq_google.api.v2.program_pb2.CircuitOperation
]
Disambiguation for operation serialization.
supported_internal_types
View source
supported_internal_types() -> Tuple
with_added_types
View source
with_added_types(
*,
gate_set_name: Optional[str] = None,
serializers: Iterable[cirq_google.serialization.op_serializer.OpSerializer
] = (),
deserializers: Iterable[cirq_google.serialization.op_deserializer.OpDeserializer
] = ()
) -> 'SerializableGateSet'
Creates a new gateset with additional (de)serializers.
Args
gate_set_name
Optional new name of the gateset. If not given, use
the same name as this gateset.
serializers
Serializers to add to those in this gateset.
deserializers
Deserializers to add to those in this gateset.