Skip to content

Commit

Permalink
Remove non-deterministic hash from circuit diagram. (quantumlib#4712)
Browse files Browse the repository at this point in the history
* Remove non-deterministic hash from circuit diagram.

* Fix CI issues.

Co-authored-by: Nate Thompson <thanacles@google.com>
  • Loading branch information
2 people authored and rht committed May 1, 2023
1 parent 72deee7 commit 98cc80c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 46 deletions.
7 changes: 3 additions & 4 deletions cirq-core/cirq/circuits/circuit_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,8 @@ def __repr__(self):

def __str__(self):
# TODO: support out-of-line subcircuit definition in string format.
header = self.circuit.diagram_name() + ':'
msg_lines = str(self.circuit).split('\n')
msg_width = max([len(header) - 4] + [len(line) for line in msg_lines])
msg_width = max([len(line) for line in msg_lines])
circuit_msg = '\n'.join(
'[ {line:<{width}} ]'.format(line=line, width=msg_width) for line in msg_lines
)
Expand All @@ -305,8 +304,8 @@ def dict_str(d: Dict) -> str:
# Only add loops if we haven't added repetition_ids.
args.append(f'loops={self.repetitions}')
if not args:
return f'{header}\n{circuit_msg}'
return f'{header}\n{circuit_msg}({", ".join(args)})'
return circuit_msg
return f'{circuit_msg}({", ".join(args)})'

def __hash__(self):
if self._hash is None:
Expand Down
25 changes: 8 additions & 17 deletions cirq-core/cirq/circuits/circuit_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,7 @@ def test_string_format():

fc0 = cirq.FrozenCircuit()
op0 = cirq.CircuitOperation(fc0)
assert (
str(op0)
== f"""\
{op0.circuit.diagram_name()}:
[ ]"""
)
assert str(op0) == f"[ ]"

fc0_global_phase_inner = cirq.FrozenCircuit(
cirq.GlobalPhaseOperation(1j), cirq.GlobalPhaseOperation(1j)
Expand All @@ -339,18 +334,16 @@ def test_string_format():
assert (
str(op0_global_phase_outer)
== f"""\
{op0_global_phase_outer.circuit.diagram_name()}:
[ ]
[ ]
[ global phase: -0.5π ]"""
[ ]
[ ]
[ global phase: -0.5π ]"""
)

fc1 = cirq.FrozenCircuit(cirq.X(x), cirq.H(y), cirq.CX(y, z), cirq.measure(x, y, z, key='m'))
op1 = cirq.CircuitOperation(fc1)
assert (
str(op1)
== f"""\
{op1.circuit.diagram_name()}:
[ 0: ───X───────M('m')─── ]
[ │ ]
[ 1: ───H───@───M──────── ]
Expand Down Expand Up @@ -387,10 +380,9 @@ def test_string_format():
assert (
str(op2)
== f"""\
{op2.circuit.diagram_name()}:
[ 0: ───X───X─── ]
[ │ ]
[ 1: ───H───@─── ](qubit_map={{1: 2}}, parent_path=('outer', 'inner'),\
[ 0: ───X───X─── ]
[ │ ]
[ 1: ───H───@─── ](qubit_map={{1: 2}}, parent_path=('outer', 'inner'),\
repetition_ids=['a', 'b', 'c'])"""
)
assert (
Expand Down Expand Up @@ -427,8 +419,7 @@ def test_string_format():
assert (
str(op3)
== f"""\
{op3.circuit.diagram_name()}:
[ 0: ───X^b───M('m')─── ](qubit_map={{0: 1}}, \
[ 0: ───X^b───M('m')─── ](qubit_map={{0: 1}}, \
key_map={{m: p}}, params={{b: 2}})"""
)
assert (
Expand Down
30 changes: 14 additions & 16 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,13 @@ def test_control_key_diagram_subcircuit():
cirq.testing.assert_has_diagram(
c,
"""
Circuit_0xfba37d11898c0e81:
[ 0: ───M─────── ]
0: ───[ ║ ]───
[ 1: ───╫───X─── ]
[ ║ ║ ]
[ a: ═══@═══^═══ ]
[ 0: ───M─────── ]
[ ║ ]
0: ───[ 1: ───╫───X─── ]───
[ ║ ║ ]
[ a: ═══@═══^═══ ]
1: ───#2────────────────────────────
1: ───#2───────────────────
""",
use_unicode_characters=True,
)
Expand All @@ -418,16 +417,15 @@ def test_control_key_diagram_subcircuit_layered():
cirq.testing.assert_has_diagram(
c,
"""
Circuit_0xa3bc42bd21c25cca:
[ 0: ───M─────── ]
0: ───M───[ ║ ]───────
║ [ 1: ───╫───X─── ]
║ [ ║ ║ ]
║ [ a: ═══@═══^═══ ]
[ 0: ───M─────── ]
[ ║ ]
0: ───M───[ 1: ───╫───X─── ]───────
║ [ ║ ║ ]
║ [ a: ═══@═══^═══ ]
║ ║
1: ───╫───#2────────────────────────────X───
║ ║
a: ═══@═══╩═════════════════════════════^═══
1: ───╫───#2───────────────────X───
║ ║ ║
a: ═══@═══╩════════════════════^═══
""",
use_unicode_characters=True,
)
Expand Down
5 changes: 0 additions & 5 deletions cirq-core/cirq/circuits/frozen_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ def device(self) -> devices.Device:
def __hash__(self):
return hash((self.moments, self.device))

def diagram_name(self):
"""Name used to represent this in circuit diagrams."""
key = hash(self) & 0xFFFF_FFFF_FFFF_FFFF
return f'Circuit_0x{key:016x}'

# Memoized methods for commonly-retrieved properties.

def _num_qubits_(self) -> int:
Expand Down
5 changes: 1 addition & 4 deletions cirq-core/cirq/testing/circuit_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,7 @@ def assert_has_diagram(
actual_diagram = actual.to_text_diagram(**kwargs).lstrip("\n").rstrip()
desired_diagram = desired.lstrip("\n").rstrip()

def remove_subcircuit_labels(s: str):
return ''.join(line for line in s.splitlines() if 'Circuit_0x' not in line)

assert remove_subcircuit_labels(actual_diagram) == remove_subcircuit_labels(desired_diagram), (
assert actual_diagram == desired_diagram, (
"Circuit's text diagram differs from the desired diagram.\n"
'\n'
'Diagram of actual circuit:\n'
Expand Down

0 comments on commit 98cc80c

Please sign in to comment.