Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cirq-google/cirq_google/api/v2/program.proto
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ message Operation {
IdentityGate identitygate = 19;
HPowGate hpowgate = 20;
SingleQubitCliffordGate singlequbitcliffordgate = 21;
ResetGate resetgate = 24;
}

// Map from the argument name to the Argument needed to fully specify
Expand Down Expand Up @@ -617,3 +618,14 @@ message IdentityGate {
message HPowGate {
FloatArg exponent = 1;
}

message ResetGate {
// Type of reset to be executed (hardware dependent)
// Internal users should use the name of the class.
// (Note that this is not used for public-facing circuits,
// which will default to cirq.ResetChannel)
string reset_type = 1;
Comment thread
pavoljuhas marked this conversation as resolved.

// Additional arguments that can be sent to the reset implementation.
map<string, Arg> arguments = 2;
}
170 changes: 88 additions & 82 deletions cirq-google/cirq_google/api/v2/program_pb2.py

Large diffs are not rendered by default.

54 changes: 51 additions & 3 deletions cirq-google/cirq_google/api/v2/program_pb2.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions cirq-google/cirq_google/serialization/circuit_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ def _serialize_gate_op(
arg_func_langs.float_arg_to_proto(
gate.duration.total_nanos(), out=msg.waitgate.duration_nanos
)
elif isinstance(gate, cirq.ResetChannel):
arg_func_langs.arg_to_proto(gate.dimension, out=msg.resetgate.arguments['dimension'])
elif isinstance(gate, CouplerPulse):
arg_func_langs.float_arg_to_proto(
gate.hold_time.total_picos(), out=msg.couplerpulsegate.hold_time_ps
Expand Down Expand Up @@ -648,6 +650,14 @@ def _deserialize_gate_op(
operation_proto.waitgate.duration_nanos, required_arg_name=None
)
op = cirq.WaitGate(duration=cirq.Duration(nanos=total_nanos or 0.0))(*qubits)
elif which_gate_type == 'resetgate':
dimensions = arg_func_langs.arg_from_proto(
operation_proto.resetgate.arguments.get('dimension', 2)
)
if not isinstance(dimensions, int):
# This should always be int, if serialized from cirq.
raise ValueError(f"dimensions {dimensions} for ResetChannel must be an integer!")
op = cirq.ResetChannel(dimension=dimensions)(*qubits)
elif which_gate_type == 'internalgate':
op = arg_func_langs.internal_gate_from_proto(operation_proto.internalgate)(*qubits)
elif which_gate_type == 'couplerpulsegate':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ def circuit_proto(json: Dict, qubits: List[str]):
{'waitgate': {'duration_nanos': {'float_value': 15}}, 'qubit_constant_index': [0]}
),
),
(
cirq.R(Q0),
op_proto(
{
'resetgate': {'arguments': {'dimension': {'arg_value': {'float_value': 2}}}},
'qubit_constant_index': [0],
}
),
),
(
cirq.MeasurementGate(num_qubits=2, key='iron', invert_mask=(True, False))(Q0, Q1),
op_proto(
Expand Down Expand Up @@ -1006,3 +1015,22 @@ def test_custom_serializer(use_constants_table: bool):
assert isinstance(op.gate, BingBongGate)
assert op.gate.param == 2.5
assert op.qubits == (cirq.q(0, 0),)


def test_reset_gate_with_improper_argument():
serializer = cg.CircuitSerializer()

op = v2.program_pb2.Operation()
op.resetgate.arguments['dimension'].arg_value.float_value = 2.5
op.qubit_constant_index.append(0)
circuit_proto = v2.program_pb2.Program(
language=v2.program_pb2.Language(arg_function_language='exp', gate_set=_SERIALIZER_NAME),
circuit=v2.program_pb2.Circuit(
scheduling_strategy=v2.program_pb2.Circuit.MOMENT_BY_MOMENT,
moments=[v2.program_pb2.Moment(operations=[op])],
),
constants=[v2.program_pb2.Constant(qubit=v2.program_pb2.Qubit(id='1_2'))],
)

with pytest.raises(ValueError, match="must be an integer"):
serializer.deserialize(circuit_proto)