From 98cc80c6d83346896e185e83e4cf077b61ab2045 Mon Sep 17 00:00:00 2001 From: Nathanael Thompson Date: Mon, 29 Nov 2021 09:17:25 -0800 Subject: [PATCH] Remove non-deterministic hash from circuit diagram. (#4712) * Remove non-deterministic hash from circuit diagram. * Fix CI issues. Co-authored-by: Nate Thompson --- cirq-core/cirq/circuits/circuit_operation.py | 7 ++--- .../cirq/circuits/circuit_operation_test.py | 25 +++++----------- cirq-core/cirq/circuits/circuit_test.py | 30 +++++++++---------- cirq-core/cirq/circuits/frozen_circuit.py | 5 ---- cirq-core/cirq/testing/circuit_compare.py | 5 +--- 5 files changed, 26 insertions(+), 46 deletions(-) diff --git a/cirq-core/cirq/circuits/circuit_operation.py b/cirq-core/cirq/circuits/circuit_operation.py index 0912f1f77cf..5dc54b68dae 100644 --- a/cirq-core/cirq/circuits/circuit_operation.py +++ b/cirq-core/cirq/circuits/circuit_operation.py @@ -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 ) @@ -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: diff --git a/cirq-core/cirq/circuits/circuit_operation_test.py b/cirq-core/cirq/circuits/circuit_operation_test.py index 1ef8fea4444..6574e45e4b0 100644 --- a/cirq-core/cirq/circuits/circuit_operation_test.py +++ b/cirq-core/cirq/circuits/circuit_operation_test.py @@ -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) @@ -339,10 +334,9 @@ 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')) @@ -350,7 +344,6 @@ def test_string_format(): assert ( str(op1) == f"""\ -{op1.circuit.diagram_name()}: [ 0: ───X───────M('m')─── ] [ │ ] [ 1: ───H───@───M──────── ] @@ -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 ( @@ -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 ( diff --git a/cirq-core/cirq/circuits/circuit_test.py b/cirq-core/cirq/circuits/circuit_test.py index 13b30675f9e..582413096f3 100644 --- a/cirq-core/cirq/circuits/circuit_test.py +++ b/cirq-core/cirq/circuits/circuit_test.py @@ -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, ) @@ -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, ) diff --git a/cirq-core/cirq/circuits/frozen_circuit.py b/cirq-core/cirq/circuits/frozen_circuit.py index d5736311fe2..7deefe2f7e7 100644 --- a/cirq-core/cirq/circuits/frozen_circuit.py +++ b/cirq-core/cirq/circuits/frozen_circuit.py @@ -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: diff --git a/cirq-core/cirq/testing/circuit_compare.py b/cirq-core/cirq/testing/circuit_compare.py index a282372ff2d..e3b7236d9e8 100644 --- a/cirq-core/cirq/testing/circuit_compare.py +++ b/cirq-core/cirq/testing/circuit_compare.py @@ -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'