Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdentityGate Visualization #6768

Closed
glanzz opened this issue Oct 15, 2024 · 2 comments · Fixed by #6926 · May be fixed by #6884
Closed

IdentityGate Visualization #6768

glanzz opened this issue Oct 15, 2024 · 2 comments · Fixed by #6926 · May be fixed by #6884
Assignees
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@glanzz
Copy link

glanzz commented Oct 15, 2024

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))
File "/Users/xxx/identity_gate_cirq.py", line 11, in <module>
    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
@glanzz glanzz added the kind/bug-report Something doesn't seem to work. label Oct 15, 2024
@NoureldinYosri NoureldinYosri added triage/discuss Needs decision / discussion, bring these up during Cirq Cynque triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Oct 15, 2024
@mhucka
Copy link
Contributor

mhucka commented Oct 16, 2024

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.

@daxfohl
Copy link
Collaborator

daxfohl commented Dec 27, 2024

I feel like we've seen situations where these are created in decomposition or transformers or whatever. Even if they're just temporary and eventually removed from the circuit, disallowing them from even being constructed could break things in ways that may be painful to work around.

In the case of diagrams, I know that PauliString returns NotImplemented from _circuit_diagram_info_ if it's empty, which causes the gate diagram to be placed by itself under all the qubits, similar to a GlobalPhaseGate. So, perhaps do the same for the I gate. Or perhaps it's possible to generalize the behavior of zero-qubit gates from whatever calls into _circuit_diagram_info_, so that not every varadic gate has to explicitly implement this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
5 participants