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

Modify IdentityGate to reject input when num_qubits is 0 #6884

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ def __init__(
applies to. The default is 2 for every qubit.

Raises:
ValueError: If the length of qid_shape doesn't equal num_qubits, or
neither `num_qubits` or `qid_shape` is supplied.
ValueError: If any of the following conditions are met:
- The length of `qid_shape` does not equal `num_qubits`.
- Neither `num_qubits` nor `qid_shape` is provided.
- `num_qubits` is 0.

"""
if num_qubits == 0:
raise ValueError('Cannot create an identity gate on 0 qubits.')
if qid_shape is None:
if num_qubits is None:
raise ValueError('Specify either the num_qubits or qid_shape argument.')
Expand Down
2 changes: 2 additions & 0 deletions cirq-core/cirq/ops/identity_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def test_identity_init(num_qubits):
cirq.IdentityGate(5, qid_shape=(1, 2))
with pytest.raises(ValueError, match='Specify either'):
cirq.IdentityGate()
with pytest.raises(ValueError, match='Cannot create an identity gate on 0 qubits.'):
cirq.IdentityGate(0)


def test_identity_on_each():
Expand Down