quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.29k stars 1.02k forks source link

IdentityGate Visualization #6768

Open glanzz opened 1 month ago

glanzz commented 1 month ago

Description of the issue IdentityGate constructor accepts a single parameter which is the number of qubits it acts on. When the parameter value is set to 0, the circuit works fine until the circuit is printed. Program crashes with an ValueError.

How to reproduce the issue


import cirq as c

idgate = c.IdentityGate(0)

qubit = c.LineQubit(0)
circuit = c.Circuit()
print(idgate())
circuit.append(idgate())

print(circuit)
circuit += c.inverse(circuit)
circuit.append(c.measure_each(qubit))

print(c.Simulator().run(circuit, repetitions=100))
```bash File "/Users/xxx/identity_gate_cirq.py", line 11, in print(circuit) File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/circuits/circuit.py", line 285, in __str__ return self.to_text_diagram() ^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/circuits/circuit.py", line 1197, in to_text_diagram diagram = self.to_text_diagram_drawer( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/circuits/circuit.py", line 1276, in to_text_diagram_drawer _draw_moment_in_diagram( File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/circuits/circuit.py", line 2612, in _draw_moment_in_diagram _draw_moment_annotations( File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/circuits/circuit.py", line 2543, in _draw_moment_annotations info = get_circuit_diagram_info(annotation, args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/cirqenv/lib/python3.11/site-packages/cirq/protocols/circuit_diagram_info_protocol.py", line 345, in _op_info_with_fallback raise ValueError(f'Wanted diagram info from {op!r} for {rows!r}) but got {info!r}') ValueError: Wanted diagram info from cirq.IdentityGate(0)() for []) but got cirq.CircuitDiagramInfo(wire_symbols=(), exponent=1, connected=True, exponent_qubit_index=None, auto_exponent_parens=True) ```

Cirq version You can get the cirq version by printing cirq.__version__. From the command line:

Cirq: 1.4.1
mhucka commented 1 month ago

Discussed during bit.ly/cirq-cynq today (2024-10-16): probably the solution is to disallow 0 qubit inputs and add validation to IdentityGate, because this is a situation that doesn't make sense physically.