-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
BooleanHamiltonian.with_qubits is broken #4390
Comments
Related to this, I would expect this identity to hold for any operation type but op.with_qubits(*op.qubits) == op |
Thanks for opening this @cduck! |
Ack |
It looks like the documentation could be more descriptive about how I think the fix is much better but it does implicitly rely on key order in q0, q1 = cirq.LineQubit.range(2)
op1 = cirq.BooleanHamiltonian(
{'a': q0, 'b': q1},
['a'],
0.1,
)
op2 = cirq.BooleanHamiltonian(
{'b': q1, 'a': q0}, # Same mapping but in a different order
['a'],
0.1,
)
new_qubits = cirq.NamedQubit.range(2, prefix='q')
new_op1 = op1.with_qubits(*new_qubits) # Map: {'a': cirq.NamedQubit('q0'), 'b': cirq.NamedQubit('q1')}
new_op2 = op2.with_qubits(*new_qubits) # Map: {'b': cirq.NamedQubit('q0'), 'a': cirq.NamedQubit('q1')}
assert new_op1 != new_op2 # These represent the same Hamiltonian but the qubits are in different orders
# Should these still be treated as equal?
assert op1 == op2 |
#4396) The current code at head does not implement BooleanHamiltonian.with_qubits() correctly. It assumes that the qubits are named and then maps them to the variable name. This is restrictive, does not follow the other classes' with_qubits(). It is also incorrect because it silently accepts a list of qubits with missing entries (e.g. there is a variable name 'a' but we don't provide a qubit named 'a'), and the error is thrown only when decompose_once() is called. Instead, here the code accepts a list of qubits and replaces them in the map. It requires that there are as many qubits provided as there were when building the object. Also, it relies on the order of the qubit keys when building the BooleanHamiltonian object, but that is currently a restriction of the API. Fixes #4390.
Sorry for pushing - I haven't seen your comment here @cduck - technically the implementation is now correct based on the documentation as the order of However, I see your point regarding insertion order being counterintuitive - but it doesn't just impact
|
Yes and thanks for the fix. I don't mean to be so pedantic about the API. I don't think the ordering is causing problems right now. If I find it does, I'll open an issue. |
quantumlib#4396) The current code at head does not implement BooleanHamiltonian.with_qubits() correctly. It assumes that the qubits are named and then maps them to the variable name. This is restrictive, does not follow the other classes' with_qubits(). It is also incorrect because it silently accepts a list of qubits with missing entries (e.g. there is a variable name 'a' but we don't provide a qubit named 'a'), and the error is thrown only when decompose_once() is called. Instead, here the code accepts a list of qubits and replaces them in the map. It requires that there are as many qubits provided as there were when building the object. Also, it relies on the order of the qubit keys when building the BooleanHamiltonian object, but that is currently a restriction of the API. Fixes quantumlib#4390.
quantumlib#4396) The current code at head does not implement BooleanHamiltonian.with_qubits() correctly. It assumes that the qubits are named and then maps them to the variable name. This is restrictive, does not follow the other classes' with_qubits(). It is also incorrect because it silently accepts a list of qubits with missing entries (e.g. there is a variable name 'a' but we don't provide a qubit named 'a'), and the error is thrown only when decompose_once() is called. Instead, here the code accepts a list of qubits and replaces them in the map. It requires that there are as many qubits provided as there were when building the object. Also, it relies on the order of the qubit keys when building the BooleanHamiltonian object, but that is currently a restriction of the API. Fixes quantumlib#4390.
Description of the issue
with_qubits
is meant to return a new operation with its (ordered) qubits substituted with a new list of qubits.BooleanHamiltonian.with_qubits
casts its argument toNamedQubit
and otherwise fails (throwsAttributeError
).BooleanHamiltonian.with_qubits
also assumes the names of the named qubits correspond to the names of the boolean variables. This won't be the case in general, especially if the user is constructing a larger circuit.How to reproduce the issue
with_qubits(*non_named_qubits)
:with_qubits(mismatched_named_qubits)
:Cirq version
0.12.0.dev (after #4309)
The text was updated successfully, but these errors were encountered: