cirq_google.serialization.CircuitSerializer

A class for serializing and deserializing programs and operations.

Inherits From: Serializer

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.

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.

name The name of the serializer.

Methods

deserialize

View source

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 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 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 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 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.