Skip to content

Commit cb60bba

Browse files
authored
Add num_qubits to DepolarizingChannel cirq_google serialization (#7502)
- Currently, serialization fails if more than 1 qubit is given.
1 parent bdcf704 commit cb60bba

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

cirq-google/cirq_google/serialization/circuit_serializer.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def _serialize_gate_op(
288288
arg_func_langs.float_arg_to_proto(
289289
gate.p, out=msg.noisechannel.depolarizingchannel.probability
290290
)
291+
msg.noisechannel.depolarizingchannel.num_qubits = len(op.qubits)
291292
elif isinstance(gate, cirq.RandomGateChannel):
292293
arg_func_langs.float_arg_to_proto(
293294
gate.probability, out=msg.noisechannel.randomgatechannel.probability
@@ -763,9 +764,14 @@ def _deserialize_gate_op(
763764
)
764765
if not isinstance(p, float):
765766
raise ValueError(
766-
f"Depolarizing noise probability {p} " "cannot be symbol or None"
767+
f"Depolarizing noise probability {p} cannot be symbol or None"
767768
) # pragma: nocover
768-
op = cirq.DepolarizingChannel(p=p)(*qubits)
769+
num_qubits = operation_proto.noisechannel.depolarizingchannel.num_qubits
770+
if num_qubits <= 0:
771+
raise ValueError(
772+
f"Depolarizing noise gate must have positive num_qubits: {num_qubits}"
773+
) # pragma: nocover
774+
op = cirq.DepolarizingChannel(p=p, n_qubits=num_qubits)(*qubits)
769775
elif which_channel_type == 'randomgatechannel':
770776
p = arg_func_langs.float_arg_from_proto(
771777
operation_proto.noisechannel.randomgatechannel.probability

cirq-google/cirq_google/serialization/circuit_serializer_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,24 @@ def circuit_proto(json: dict, qubits: list[str]):
390390
cirq.depolarize(0.5)(Q0),
391391
op_proto(
392392
{
393-
'noisechannel': {'depolarizingchannel': {'probability': {'float_value': 0.5}}},
393+
'noisechannel': {
394+
'depolarizingchannel': {'probability': {'float_value': 0.5}, 'num_qubits': 1}
395+
},
394396
'qubit_constant_index': [0],
395397
}
396398
),
397399
),
400+
(
401+
cirq.depolarize(0.5, n_qubits=2)(Q0, Q1),
402+
op_proto(
403+
{
404+
'noisechannel': {
405+
'depolarizingchannel': {'probability': {'float_value': 0.5}, 'num_qubits': 2}
406+
},
407+
'qubit_constant_index': [0, 1],
408+
}
409+
),
410+
),
398411
(
399412
cirq.X(Q0).with_probability(0.5),
400413
op_proto(

0 commit comments

Comments
 (0)