When a method expects qubits as arguments, verify that it received qubits
See original GitHub issueCurrently on_each
in SingleQubitGate
does this and then calls on
in Gate
once it is content that is has been given valid arguments. This verification should be moved to on
or validate_args
in Gate.
This is particularly relevant since the signature for on
was recently changed from expecting a list of qubits to an unpacked list of qubits. I would push for validate_args
since it covers the most cases of user error. If we are going to perform this explicit type-checking anywhere, it makes the most sense in validate_args
.
Consider the following example:
cirq.X.on(*cirq.LineQubit.range(3))
cirq.X.on_each(*cirq.LineQubit.range(3))
The second line is a valid way to get an op_tree
of 3 operations with different qubits all acted on by an X gate. The first will raise an error because cirq.X is a SingleQubitGate
and will complain about being applied to 3 qubits.
Now consider this code:
cirq.X.on(cirq.LineQubit.range(3))
cirq.X.on_each(cirq.LineQubit.range(3))
The second line raises an error because the list of qubits we passed in isn’t a qubit. The first line doesn’t do this because it doesn’t check that it received qubits and instead only checks that the number of arguments it received is consistent with the number of qubits the gate acts on. Instead the user gets a nonsensical GateOperation
object that will give them debugging issues down the line. In fact, cirq.GateOperation(cirq.X, "Q")
will also not complain when it should.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Go for it!
Can I give this a shot?