Skip to content
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
8 changes: 6 additions & 2 deletions cirq-core/cirq/ops/diagonal_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,13 @@ def _apply_unitary_(self, args: protocols.ApplyUnitaryArgs) -> np.ndarray:
return args.target_tensor

def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
rounded_angles = np.array(self._diag_angles_radians)
rounded_angles = self._diag_angles_radians
if args.precision is not None:
rounded_angles = rounded_angles.round(args.precision)
rounded_angles = tuple(
round(angle, args.precision) if not protocols.is_parameterized(angle) else angle
for angle in rounded_angles
)

if len(rounded_angles) <= 4:
rounded_angles_str = ', '.join(proper_repr(angle) for angle in rounded_angles)
diag_str = f'diag({rounded_angles_str})'
Expand Down
25 changes: 25 additions & 0 deletions cirq-core/cirq/ops/diagonal_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ def test_diagram() -> None:
0: ───diag(2, 3, 5, 7)───
│
1: ───#2─────────────────
""",
)
parameterized_circuit = cirq.Circuit(
cirq.DiagonalGate([0.12345, 1.0, sympy.Symbol('a'), 2.0])(a, b)
)
cirq.testing.assert_has_diagram(
parameterized_circuit,
"""
0: ───diag(0.123, 1.0, sympy.Symbol('a'), 2.0)───
│
1: ───#2─────────────────────────────────────────
""",
)

parameterized_circuit_large = cirq.Circuit(
cirq.DiagonalGate([0.12345, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, sympy.Symbol('a')])(a, b, c)
)
cirq.testing.assert_has_diagram(
parameterized_circuit_large,
"""
0: ───diag(0.123, 1.0, ..., 6.0, sympy.Symbol('a'))───
│
1: ───#2──────────────────────────────────────────────
│
2: ───#3──────────────────────────────────────────────
""",
)

Expand Down
7 changes: 5 additions & 2 deletions cirq-core/cirq/ops/three_qubit_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,12 @@ def _apply_unitary_(self, args: protocols.ApplyUnitaryArgs) -> np.ndarray:
return args.target_tensor

def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
rounded_angles = np.array(self._diag_angles_radians)
rounded_angles = self._diag_angles_radians
if args.precision is not None:
rounded_angles = rounded_angles.round(args.precision)
rounded_angles = tuple(
round(angle, args.precision) if not protocols.is_parameterized(angle) else angle
for angle in rounded_angles
)
diag_str = f"diag({', '.join(proper_repr(angle) for angle in rounded_angles)})"
return protocols.CircuitDiagramInfo((diag_str, '#2', '#3'))

Expand Down
15 changes: 15 additions & 0 deletions cirq-core/cirq/ops/three_qubit_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ def test_diagram() -> None:
""",
use_unicode_characters=False,
)
parameterized_circuit = cirq.Circuit(
cirq.ThreeQubitDiagonalGate([0.12345, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, sympy.Symbol('a')])(
a, b, c
)
)
cirq.testing.assert_has_diagram(
parameterized_circuit,
"""
0: ───diag(0.123, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, sympy.Symbol('a'))───
│
1: ───#2─────────────────────────────────────────────────────────────
│
2: ───#3─────────────────────────────────────────────────────────────
""",
)


def test_diagonal_exponent() -> None:
Expand Down
7 changes: 5 additions & 2 deletions cirq-core/cirq/ops/two_qubit_diagonal_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,12 @@ def _apply_unitary_(self, args: protocols.ApplyUnitaryArgs) -> np.ndarray:
return args.target_tensor

def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
rounded_angles = np.array(self._diag_angles_radians)
rounded_angles = self._diag_angles_radians
if args.precision is not None:
rounded_angles = rounded_angles.round(args.precision)
rounded_angles = tuple(
round(angle, args.precision) if not protocols.is_parameterized(angle) else angle
for angle in rounded_angles
)
diag_str = f"diag({', '.join(proper_repr(angle) for angle in rounded_angles)})"
return protocols.CircuitDiagramInfo((diag_str, '#2'))

Expand Down
11 changes: 11 additions & 0 deletions cirq-core/cirq/ops/two_qubit_diagonal_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ def test_diagram() -> None:
""",
use_unicode_characters=False,
)
parameterized_circuit = cirq.Circuit(
cirq.TwoQubitDiagonalGate([0.12345, 1.0, sympy.Symbol('a'), 2.0])(a, b)
)
cirq.testing.assert_has_diagram(
parameterized_circuit,
"""
0: ───diag(0.123, 1.0, sympy.Symbol('a'), 2.0)───
│
1: ───#2─────────────────────────────────────────
""",
)


def test_diagonal_exponent() -> None:
Expand Down
Loading