Skip to content

Add noise gates to cirq_google serialization #7443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 26 additions & 1 deletion cirq-google/cirq_google/api/v2/program.proto
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,32 @@ message WaitGate {
FloatArg duration_nanos = 1;
}

// Representation of cirq.DepolarizingChannel
message DepolarizingChannel {
FloatArg probability = 1;
int32 num_qubits = 2;
}

// Representation of cirq.RandomGateChannel
message RandomGateChannel {
FloatArg probability = 1;
Operation sub_gate = 2;
}

// Representation of noisy channels
// These should only be used for serialization
// of noisy circuits for simulation.
// These channels would generally not be supported
// by hardware.
message NoiseChannel {
oneof channel_value {
DepolarizingChannel depolarizingchannel = 1;
RandomGateChannel randomgatechannel = 2;
}
}

// An operation acts on a set of qubits.
// next available id = 27
// next available id = 28
message Operation {
// Previously deprecated fields. Do not use.
reserved 1, 2;
Expand All @@ -241,6 +265,7 @@ message Operation {
SingleQubitCliffordGate singlequbitcliffordgate = 21;
ResetGate resetgate = 24;
ISwapLikeGate iswaplikegate = 26;
NoiseChannel noisechannel = 27;
}

// Which qubits the operation acts on.
Expand Down
192 changes: 99 additions & 93 deletions cirq-google/cirq_google/api/v2/program_pb2.py

Large diffs are not rendered by default.

86 changes: 82 additions & 4 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.

44 changes: 44 additions & 0 deletions cirq-google/cirq_google/serialization/circuit_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ def _serialize_gate_op(
arg_func_langs.float_arg_to_proto(
gate.q1_detune_mhz, out=msg.couplerpulsegate.q1_detune_mhz
)
elif isinstance(gate, cirq.DepolarizingChannel):
arg_func_langs.float_arg_to_proto(
gate.p, out=msg.noisechannel.depolarizingchannel.probability
)
elif isinstance(gate, cirq.RandomGateChannel):
arg_func_langs.float_arg_to_proto(
gate.probability, out=msg.noisechannel.randomgatechannel.probability
)
self._serialize_gate_op(
gate.sub_gate(*op.qubits),
msg=msg.noisechannel.randomgatechannel.sub_gate,
constants=constants,
raw_constants=raw_constants,
)
else:
raise ValueError(f'Cannot serialize op {op!r} of type {type(gate)}')

Expand Down Expand Up @@ -730,6 +744,36 @@ def _deserialize_gate_op(
or 0.0,
)
op = gate(*qubits)
elif which_gate_type == 'noisechannel':
which_channel_type = operation_proto.noisechannel.WhichOneof('channel_value')
if which_channel_type == 'depolarizingchannel':
p = arg_func_langs.float_arg_from_proto(
operation_proto.noisechannel.depolarizingchannel.probability
)
if not isinstance(p, float):
raise ValueError(
f"Depolarizing noise probability {p} " "cannot be symbol or None"
) # pragma: nocover
op = cirq.DepolarizingChannel(p=p)(*qubits)
elif which_channel_type == 'randomgatechannel':
p = arg_func_langs.float_arg_from_proto(
operation_proto.noisechannel.randomgatechannel.probability
)
sub_gate = self._deserialize_gate_op(
operation_proto.noisechannel.randomgatechannel.sub_gate,
constants=constants,
deserialized_constants=deserialized_constants,
)
if sub_gate is None or sub_gate.gate is None:
raise ValueError(
"Not a valid gate for RandomGateChannel: " f"{operation_proto}"
) # pragma: nocover
op = cirq.RandomGateChannel(probability=p, sub_gate=sub_gate.gate)(*qubits)
else:
raise ValueError(
f'Unsupported serialized noise channel with type "{which_channel_type}".'
f'\n\noperation_proto:\n{operation_proto}'
) # pragma: nocover
else:
raise ValueError(
f'Unsupported serialized gate with type "{which_gate_type}".'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,32 @@ def circuit_proto(json: dict, qubits: list[str]):
),
),
(cirq.I(Q0), op_proto({'identitygate': {'qid_shape': [2]}, 'qubit_constant_index': [0]})),
(
cirq.depolarize(0.5)(Q0),
op_proto(
{
'noisechannel': {'depolarizingchannel': {'probability': {'float_value': 0.5}}},
'qubit_constant_index': [0],
}
),
),
(
cirq.X(Q0).with_probability(0.5),
op_proto(
{
'noisechannel': {
'randomgatechannel': {
'probability': {'float_value': 0.5},
'sub_gate': {
'xpowgate': {'exponent': {'float_value': 1.0}},
'qubit_constant_index': [0],
},
}
},
'qubit_constant_index': [0],
}
),
),
]


Expand Down