Skip to content

Commit

Permalink
Fix DepolarizingChannel._circuit_diagram_info_ assuming it is a singl…
Browse files Browse the repository at this point in the history
…e qubit gate (#3780)
  • Loading branch information
Strilanc committed Feb 10, 2021
1 parent 942b2c9 commit e882475
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 8 additions & 3 deletions cirq/ops/common_channels.py
Expand Up @@ -310,10 +310,15 @@ def _act_on_(self, args: Any) -> bool:
return True
return NotImplemented

def _circuit_diagram_info_(self, args: 'protocols.CircuitDiagramInfoArgs') -> str:
def _circuit_diagram_info_(self, args: 'protocols.CircuitDiagramInfoArgs') -> Tuple[str, ...]:
result: Tuple[str, ...]
if args.precision is not None:
return f"D({self._p:.{args.precision}g})"
return f"D({self._p})"
result = (f"D({self._p:.{args.precision}g})",)
else:
result = (f"D({self._p})",)
while len(result) < self.num_qubits():
result += (f"#{len(result) + 1}",)
return result

@property
def p(self) -> float:
Expand Down
16 changes: 13 additions & 3 deletions cirq/ops/common_channels_test.py
Expand Up @@ -164,6 +164,16 @@ def test_depolarizing_channel_two_qubits():
)
assert cirq.has_channel(d)

assert d.num_qubits() == 2
cirq.testing.assert_has_diagram(
cirq.Circuit(d(*cirq.LineQubit.range(2))),
"""
0: ───D(0.15)───
1: ───#2────────
""",
)


def test_depolarizing_mixture():
d = cirq.depolarize(0.3)
Expand Down Expand Up @@ -275,13 +285,13 @@ def test_depolarizing_channel_text_diagram():
def test_depolarizing_channel_text_diagram_two_qubits():
d = cirq.depolarize(0.1234567, n_qubits=2)
assert cirq.circuit_diagram_info(d, args=round_to_6_prec) == cirq.CircuitDiagramInfo(
wire_symbols=('D(0.123457)',)
wire_symbols=('D(0.123457)', '#2')
)
assert cirq.circuit_diagram_info(d, args=round_to_2_prec) == cirq.CircuitDiagramInfo(
wire_symbols=('D(0.12)',)
wire_symbols=('D(0.12)', '#2')
)
assert cirq.circuit_diagram_info(d, args=no_precision) == cirq.CircuitDiagramInfo(
wire_symbols=('D(0.1234567)',)
wire_symbols=('D(0.1234567)', '#2')
)


Expand Down

0 comments on commit e882475

Please sign in to comment.