A class for serializing and deserializing programs and operations.
Inherits From: Serializer
cirq_google.serialization.CircuitSerializer(
op_serializer: (op_serializer.OpSerializer | None) = None,
op_deserializer: (op_deserializer.OpDeserializer | None) = None,
tag_serializer: (tag_serializer.TagSerializer | None) = None,
tag_deserializer: (tag_deserializer.TagDeserializer | None) = None,
**kwargs
)
This class is for serializing cirq_google.api.v2. protos using one
message type per gate type. It serializes qubits by adding a field
into the constants table. Usage is by passing a cirq.Circuit
to the serialize() method of the class, which will produce a
Program proto. Likewise, the deserialize method will produce
a cirq.Circuit object from a Program proto.
Args |
op_serializer
|
Optional custom serializer for serializing unknown gates.
|
op_deserializer
|
Optional custom deserializer for deserializing unknown gates.
|
tag_serializer
|
Optional custom serializer for serializing unknown tags.
|
tag_deserializer
|
Optional custom deserializer for deserializing unknown tags.
|
Attributes |
name
|
The name of the serializer.
|
Methods
deserialize
View source
deserialize(
proto: v2.program_pb2.Program
) -> cirq.Circuit
Deserialize a Circuit from a cirq_google.api.v2.Program.
| Args |
proto
|
A dictionary representing a cirq_google.api.v2.Program proto.
|
| Returns |
|
The deserialized Circuit
|
| Raises |
ValueError
|
If the given proto has no language or the language gate set mismatches
that specified in as the name of this serialized gate set. Also if deserializing
a schedule is attempted.
|
NotImplementedError
|
If the program proto does not contain a circuit or schedule.
|
deserialize_multi_program
View source
deserialize_multi_program(
proto: v2.program_pb2.Program
) -> Sequence[tuple[str, tuple[tuple[str, Any], ...], cirq.AbstractCircuit]]
Deserialize multiple circuits from a KeyedCircuit in a Program proto.
This unrolls the KeyedCircuit message with a Program proto into a list
of circuits. Returned is a list of tuples in the form:
(key, (args,...), circuit).
If the original input was a map, key will be populated.
If the input was a function, args will be populated with the input parameters.
If the input was a function that returned a map of circuits, then both
will be populated.
serialize
View source
serialize(
program: cirq.AbstractCircuit, msg: (v2.program_pb2.Program | None) = None
) -> 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.
|
| Raises |
NotImplementedError
|
If the program is of a type that is not supported.
|
serialize_circuit_function
View source
serialize_circuit_function(
circuit_function: (Callable[..., cirq.AbstractCircuit] | Callable[..., Mapping[str, cirq.
AbstractCircuit]]),
sweep: cirq.Sweep,
msg: (v2.program_pb2.Program | None) = None
) -> v2.program_pb2.Program
Serialize multiple related circuits to cirq_google.api.v2.Program proto.
| Args |
circuit_function
|
A function returning circuits to serialize.
This function should return a circuit or mapping from string to
a circuit. The arguments in this function should match a (subset of)
the parameters in the accompanied sweep call. The function will
be unrolled for each combination of sweep parameters.
|
sweep
|
If a function is passed in, this will be the sweep to call on the
function.
|
msg
|
An optional proto object to populate with the serialization
results.
|
| Raises |
NotImplementedError
|
If the program is of a type that is not supported.
|
ValueError
|
If the function returns something not a circuit.
|
serialize_multi_program
View source
serialize_multi_program(
multi_program: (Sequence[cirq.AbstractCircuit] | Mapping[str, cirq.AbstractCircuit]),
msg: (v2.program_pb2.Program | None) = None
) -> v2.program_pb2.Program
Serialize multiple related circuits to cirq_google.api.v2.Program proto.
| Args |
multi_program
|
A collection of circuits to serialize. This
should take the form of either a sequence of circuits (such as a list)
or a mapping from a key string to circuits.
|
msg
|
An optional proto object to populate with the serialization
results.
|
| Raises |
NotImplementedError
|
If the program is of a type that is not supported.
|
ValueError
|
If the function returns something not a circuit.
|