Skip to content

Commit 55873b9

Browse files
authored
Rename cirq.channel to cirq.kraus (#4195)
1 parent b43eaaa commit 55873b9

23 files changed

+176
-131
lines changed

cirq-core/cirq/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@
479479
definitely_commutes,
480480
equal_up_to_global_phase,
481481
has_channel,
482+
has_kraus,
482483
has_mixture,
483484
has_stabilizer_effect,
484485
has_unitary,
@@ -487,6 +488,7 @@
487488
is_parameterized,
488489
JsonResolver,
489490
json_serializable_dataclass,
491+
kraus,
490492
measurement_key,
491493
measurement_keys,
492494
mixture,

cirq-core/cirq/contrib/quimb/density_matrix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def circuit_to_density_matrix_tensors(
8484
8585
Args:
8686
circuit: The circuit containing operations that support the
87-
cirq.unitary() or cirq.channel() protocols.
87+
cirq.unitary() or cirq.kraus() protocols.
8888
qubits: The qubits in the circuit.
8989
9090
Returns:
@@ -161,8 +161,8 @@ def _positions(mi, qubits):
161161
tags={f'Q{len(op.qubits)}', f'i{mi + 1}b', _qpos_tag(op.qubits)},
162162
)
163163
)
164-
elif cirq.has_channel(op):
165-
K = np.asarray(cirq.channel(op), dtype=np.complex128)
164+
elif cirq.has_kraus(op):
165+
K = np.asarray(cirq.kraus(op), dtype=np.complex128)
166166
kraus_inds = [f'k{kraus_frontier}']
167167
tensors.append(
168168
qtn.Tensor(

cirq-core/cirq/ops/common_channels_test.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def assert_mixtures_equal(actual, expected):
5858
def test_asymmetric_depolarizing_channel():
5959
d = cirq.asymmetric_depolarize(0.1, 0.2, 0.3)
6060
np.testing.assert_almost_equal(
61-
cirq.channel(d),
61+
cirq.kraus(d),
6262
(np.sqrt(0.4) * np.eye(2), np.sqrt(0.1) * X, np.sqrt(0.2) * Y, np.sqrt(0.3) * Z),
6363
)
64-
assert cirq.has_channel(d)
64+
assert cirq.has_kraus(d)
6565

6666

6767
def test_asymmetric_depolarizing_mixture():
@@ -134,21 +134,21 @@ def test_asymmetric_depolarizing_channel_text_diagram():
134134
def test_depolarizing_channel():
135135
d = cirq.depolarize(0.3)
136136
np.testing.assert_almost_equal(
137-
cirq.channel(d),
137+
cirq.kraus(d),
138138
(
139139
np.sqrt(0.7) * np.eye(2),
140140
np.sqrt(0.1) * X,
141141
np.sqrt(0.1) * Y,
142142
np.sqrt(0.1) * Z,
143143
),
144144
)
145-
assert cirq.has_channel(d)
145+
assert cirq.has_kraus(d)
146146

147147

148148
def test_depolarizing_channel_two_qubits():
149149
d = cirq.depolarize(0.15, n_qubits=2)
150150
np.testing.assert_almost_equal(
151-
cirq.channel(d),
151+
cirq.kraus(d),
152152
(
153153
np.sqrt(0.85) * np.eye(4),
154154
np.sqrt(0.01) * np.kron(np.eye(2), X),
@@ -168,7 +168,7 @@ def test_depolarizing_channel_two_qubits():
168168
np.sqrt(0.01) * np.kron(Z, Z),
169169
),
170170
)
171-
assert cirq.has_channel(d)
171+
assert cirq.has_kraus(d)
172172

173173
assert d.num_qubits() == 2
174174
cirq.testing.assert_has_diagram(
@@ -310,15 +310,15 @@ def test_depolarizing_channel_text_diagram_two_qubits():
310310
def test_generalized_amplitude_damping_channel():
311311
d = cirq.generalized_amplitude_damp(0.1, 0.3)
312312
np.testing.assert_almost_equal(
313-
cirq.channel(d),
313+
cirq.kraus(d),
314314
(
315315
np.sqrt(0.1) * np.array([[1.0, 0.0], [0.0, np.sqrt(1.0 - 0.3)]]),
316316
np.sqrt(0.1) * np.array([[0.0, np.sqrt(0.3)], [0.0, 0.0]]),
317317
np.sqrt(0.9) * np.array([[np.sqrt(1.0 - 0.3), 0.0], [0.0, 1.0]]),
318318
np.sqrt(0.9) * np.array([[0.0, 0.0], [np.sqrt(0.3), 0.0]]),
319319
),
320320
)
321-
assert cirq.has_channel(d)
321+
assert cirq.has_kraus(d)
322322
assert not cirq.has_mixture(d)
323323

324324

@@ -376,13 +376,13 @@ def test_generalized_amplitude_damping_channel_text_diagram():
376376
def test_amplitude_damping_channel():
377377
d = cirq.amplitude_damp(0.3)
378378
np.testing.assert_almost_equal(
379-
cirq.channel(d),
379+
cirq.kraus(d),
380380
(
381381
np.array([[1.0, 0.0], [0.0, np.sqrt(1.0 - 0.3)]]),
382382
np.array([[0.0, np.sqrt(0.3)], [0.0, 0.0]]),
383383
),
384384
)
385-
assert cirq.has_channel(d)
385+
assert cirq.has_kraus(d)
386386
assert not cirq.has_mixture(d)
387387

388388

@@ -432,22 +432,22 @@ def test_amplitude_damping_channel_text_diagram():
432432
def test_reset_channel():
433433
r = cirq.reset(cirq.LineQubit(0))
434434
np.testing.assert_almost_equal(
435-
cirq.channel(r), (np.array([[1.0, 0.0], [0.0, 0]]), np.array([[0.0, 1.0], [0.0, 0.0]]))
435+
cirq.kraus(r), (np.array([[1.0, 0.0], [0.0, 0]]), np.array([[0.0, 1.0], [0.0, 0.0]]))
436436
)
437-
assert cirq.has_channel(r)
437+
assert cirq.has_kraus(r)
438438
assert not cirq.has_mixture(r)
439439
assert cirq.qid_shape(r) == (2,)
440440

441441
r = cirq.reset(cirq.LineQid(0, dimension=3))
442442
np.testing.assert_almost_equal(
443-
cirq.channel(r),
443+
cirq.kraus(r),
444444
(
445445
np.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]]),
446446
np.array([[0, 1, 0], [0, 0, 0], [0, 0, 0]]),
447447
np.array([[0, 0, 1], [0, 0, 0], [0, 0, 0]]),
448448
),
449449
) # yapf: disable
450-
assert cirq.has_channel(r)
450+
assert cirq.has_kraus(r)
451451
assert not cirq.has_mixture(r)
452452
assert cirq.qid_shape(r) == (3,)
453453

@@ -508,13 +508,13 @@ def test_reset_act_on():
508508
def test_phase_damping_channel():
509509
d = cirq.phase_damp(0.3)
510510
np.testing.assert_almost_equal(
511-
cirq.channel(d),
511+
cirq.kraus(d),
512512
(
513513
np.array([[1.0, 0.0], [0.0, np.sqrt(1 - 0.3)]]),
514514
np.array([[0.0, 0.0], [0.0, np.sqrt(0.3)]]),
515515
),
516516
)
517-
assert cirq.has_channel(d)
517+
assert cirq.has_kraus(d)
518518
assert not cirq.has_mixture(d)
519519

520520

@@ -564,9 +564,9 @@ def test_phase_damping_channel_text_diagram():
564564
def test_phase_flip_channel():
565565
d = cirq.phase_flip(0.3)
566566
np.testing.assert_almost_equal(
567-
cirq.channel(d), (np.sqrt(1.0 - 0.3) * np.eye(2), np.sqrt(0.3) * Z)
567+
cirq.kraus(d), (np.sqrt(1.0 - 0.3) * np.eye(2), np.sqrt(0.3) * Z)
568568
)
569-
assert cirq.has_channel(d)
569+
assert cirq.has_kraus(d)
570570

571571

572572
def test_phase_flip_mixture():
@@ -628,9 +628,9 @@ def test_phase_flip_channel_text_diagram():
628628
def test_bit_flip_channel():
629629
d = cirq.bit_flip(0.3)
630630
np.testing.assert_almost_equal(
631-
cirq.channel(d), (np.sqrt(1.0 - 0.3) * np.eye(2), np.sqrt(0.3) * X)
631+
cirq.kraus(d), (np.sqrt(1.0 - 0.3) * np.eye(2), np.sqrt(0.3) * X)
632632
)
633-
assert cirq.has_channel(d)
633+
assert cirq.has_kraus(d)
634634

635635

636636
def test_bit_flip_mixture():
@@ -734,9 +734,9 @@ def test_missing_prob_mass():
734734
def test_multi_asymmetric_depolarizing_channel():
735735
d = cirq.asymmetric_depolarize(error_probabilities={'II': 0.8, 'XX': 0.2})
736736
np.testing.assert_almost_equal(
737-
cirq.channel(d), (np.sqrt(0.8) * np.eye(4), np.sqrt(0.2) * np.kron(X, X))
737+
cirq.kraus(d), (np.sqrt(0.8) * np.eye(4), np.sqrt(0.2) * np.kron(X, X))
738738
)
739-
assert cirq.has_channel(d)
739+
assert cirq.has_kraus(d)
740740
np.testing.assert_equal(d._num_qubits_(), 2)
741741

742742
with pytest.raises(ValueError, match="num_qubits should be 1"):

cirq-core/cirq/ops/gate_operation_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ def test_unitary():
249249
def test_channel():
250250
a = cirq.NamedQubit('a')
251251
op = cirq.bit_flip(0.5).on(a)
252-
np.testing.assert_allclose(cirq.channel(op), cirq.channel(op.gate))
253-
assert cirq.has_channel(op)
252+
np.testing.assert_allclose(cirq.kraus(op), cirq.kraus(op.gate))
253+
assert cirq.has_kraus(op)
254254

255-
assert cirq.channel(cirq.SingleQubitGate()(a), None) is None
256-
assert not cirq.has_channel(cirq.SingleQubitGate()(a))
255+
assert cirq.kraus(cirq.SingleQubitGate()(a), None) is None
256+
assert not cirq.has_kraus(cirq.SingleQubitGate()(a))
257257

258258

259259
def test_measurement_key():

cirq-core/cirq/ops/measurement_gate_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ def test_measurement_gate_diagram():
201201

202202
def test_measurement_channel():
203203
np.testing.assert_allclose(
204-
cirq.channel(cirq.MeasurementGate(1)),
204+
cirq.kraus(cirq.MeasurementGate(1)),
205205
(np.array([[1, 0], [0, 0]]), np.array([[0, 0], [0, 1]])),
206206
)
207207
# yapf: disable
208208
np.testing.assert_allclose(
209-
cirq.channel(cirq.MeasurementGate(2)),
209+
cirq.kraus(cirq.MeasurementGate(2)),
210210
(np.array([[1, 0, 0, 0],
211211
[0, 0, 0, 0],
212212
[0, 0, 0, 0],
@@ -224,7 +224,7 @@ def test_measurement_channel():
224224
[0, 0, 0, 0],
225225
[0, 0, 0, 1]])))
226226
np.testing.assert_allclose(
227-
cirq.channel(cirq.MeasurementGate(2, qid_shape=(2, 3))),
227+
cirq.kraus(cirq.MeasurementGate(2, qid_shape=(2, 3))),
228228
(np.diag([1, 0, 0, 0, 0, 0]),
229229
np.diag([0, 1, 0, 0, 0, 0]),
230230
np.diag([0, 0, 1, 0, 0, 0]),

cirq-core/cirq/ops/random_gate_channel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _has_mixture_(self):
5757
return not self._is_parameterized_() and protocols.has_mixture(self.sub_gate)
5858

5959
def _has_kraus_(self):
60-
return not self._is_parameterized_() and protocols.has_channel(self.sub_gate)
60+
return not self._is_parameterized_() and protocols.has_kraus(self.sub_gate)
6161

6262
def _is_parameterized_(self) -> bool:
6363
return protocols.is_parameterized(self.probability) or protocols.is_parameterized(
@@ -94,7 +94,7 @@ def _kraus_(self):
9494
if self._is_parameterized_():
9595
return NotImplemented
9696

97-
channel = protocols.channel(self.sub_gate, None)
97+
channel = protocols.kraus(self.sub_gate, None)
9898
if channel is None:
9999
return NotImplemented
100100

cirq-core/cirq/ops/random_gate_channel_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ def num_qubits(self) -> int:
125125
def test_parameterized(resolve_fn):
126126
op = cirq.X.with_probability(sympy.Symbol('x'))
127127
assert cirq.is_parameterized(op)
128-
assert not cirq.has_channel(op)
128+
assert not cirq.has_kraus(op)
129129
assert not cirq.has_mixture(op)
130130

131131
op2 = resolve_fn(op, {'x': 0.5})
132132
assert op2 == cirq.X.with_probability(0.5)
133133
assert not cirq.is_parameterized(op2)
134-
assert cirq.has_channel(op2)
134+
assert cirq.has_kraus(op2)
135135
assert cirq.has_mixture(op2)
136136

137137

@@ -158,7 +158,7 @@ def num_qubits(self) -> int:
158158

159159

160160
def assert_channel_sums_to_identity(val):
161-
m = cirq.channel(val)
161+
m = cirq.kraus(val)
162162
s = sum(np.conj(e.T) @ e for e in m)
163163
np.testing.assert_allclose(s, np.eye(np.product(cirq.qid_shape(val))), atol=1e-8)
164164

@@ -168,14 +168,14 @@ class NoDetailsGate(cirq.Gate):
168168
def num_qubits(self) -> int:
169169
return 1
170170

171-
assert not cirq.has_channel(NoDetailsGate().with_probability(0.5))
172-
assert cirq.channel(NoDetailsGate().with_probability(0.5), None) is None
173-
assert cirq.channel(cirq.X.with_probability(sympy.Symbol('x')), None) is None
171+
assert not cirq.has_kraus(NoDetailsGate().with_probability(0.5))
172+
assert cirq.kraus(NoDetailsGate().with_probability(0.5), None) is None
173+
assert cirq.kraus(cirq.X.with_probability(sympy.Symbol('x')), None) is None
174174
assert_channel_sums_to_identity(cirq.X.with_probability(0.25))
175175
assert_channel_sums_to_identity(cirq.bit_flip(0.75).with_probability(0.25))
176176
assert_channel_sums_to_identity(cirq.amplitude_damp(0.75).with_probability(0.25))
177177

178-
m = cirq.channel(cirq.X.with_probability(0.25))
178+
m = cirq.kraus(cirq.X.with_probability(0.25))
179179
assert len(m) == 2
180180
np.testing.assert_allclose(
181181
m[0],
@@ -188,7 +188,7 @@ def num_qubits(self) -> int:
188188
atol=1e-8,
189189
)
190190

191-
m = cirq.channel(cirq.bit_flip(0.75).with_probability(0.25))
191+
m = cirq.kraus(cirq.bit_flip(0.75).with_probability(0.25))
192192
assert len(m) == 3
193193
np.testing.assert_allclose(
194194
m[0],
@@ -206,7 +206,7 @@ def num_qubits(self) -> int:
206206
atol=1e-8,
207207
)
208208

209-
m = cirq.channel(cirq.amplitude_damp(0.75).with_probability(0.25))
209+
m = cirq.kraus(cirq.amplitude_damp(0.75).with_probability(0.25))
210210
assert len(m) == 3
211211
np.testing.assert_allclose(
212212
m[0],

cirq-core/cirq/ops/raw_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,10 @@ def _mixture_(self) -> Sequence[Tuple[float, Any]]:
656656
return protocols.mixture(self.sub_operation, NotImplemented)
657657

658658
def _has_kraus_(self) -> bool:
659-
return protocols.has_channel(self.sub_operation)
659+
return protocols.has_kraus(self.sub_operation)
660660

661661
def _kraus_(self) -> Union[Tuple[np.ndarray], NotImplementedType]:
662-
return protocols.channel(self.sub_operation, NotImplemented)
662+
return protocols.kraus(self.sub_operation, NotImplemented)
663663

664664
def _measurement_key_(self) -> str:
665665
return protocols.measurement_key(self.sub_operation, NotImplemented)

cirq-core/cirq/ops/raw_types_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ def test_tagged_operation_forwards_protocols():
589589
assert cirq.decompose(tagged_h) == cirq.decompose(h)
590590
assert cirq.pauli_expansion(tagged_h) == cirq.pauli_expansion(h)
591591
assert cirq.equal_up_to_global_phase(h, tagged_h)
592-
assert np.isclose(cirq.channel(h), cirq.channel(tagged_h)).all()
592+
assert np.isclose(cirq.kraus(h), cirq.kraus(tagged_h)).all()
593593

594594
assert cirq.measurement_key(cirq.measure(q1, key='blah').with_tags(tag)) == 'blah'
595595

@@ -631,7 +631,7 @@ def test_tagged_operation_forwards_protocols():
631631
flip = cirq.bit_flip(0.5)(q1)
632632
tagged_flip = cirq.bit_flip(0.5)(q1).with_tags(tag)
633633
assert cirq.has_mixture(tagged_flip)
634-
assert cirq.has_channel(tagged_flip)
634+
assert cirq.has_kraus(tagged_flip)
635635

636636
flip_mixture = cirq.mixture(flip)
637637
tagged_mixture = cirq.mixture(tagged_flip)

cirq-core/cirq/protocols/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
)
4141
from cirq.protocols.channel import (
4242
channel,
43+
kraus,
4344
has_channel,
45+
has_kraus,
4446
SupportsChannel,
47+
SupportsKraus,
4548
)
4649
from cirq.protocols.commutes_protocol import (
4750
commutes,

cirq-core/cirq/protocols/apply_channel_protocol.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
apply_unitary,
2525
ApplyUnitaryArgs,
2626
)
27-
from cirq.protocols.channel import channel
27+
from cirq.protocols.channel import kraus
2828
from cirq.protocols import qid_shape_protocol
2929
from cirq.type_workarounds import NotImplementedType
3030

@@ -257,9 +257,9 @@ def err_str(buf_num_str):
257257
return result
258258

259259
# Fallback to using the object's `_kraus_` matrices.
260-
kraus = channel(val, None)
261-
if kraus is not None:
262-
return _apply_kraus(kraus, args)
260+
ks = kraus(val, None)
261+
if ks is not None:
262+
return _apply_kraus(ks, args)
263263

264264
# Don't know how to apply channel. Fallback to specified default behavior.
265265
if default is not RaiseTypeErrorIfNotProvided:

0 commit comments

Comments
 (0)