Skip to content

Giving inconsistent measurement key qubits gives unequal repr #4273

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

Closed
Strilanc opened this issue Jun 29, 2021 · 5 comments
Closed

Giving inconsistent measurement key qubits gives unequal repr #4273

Strilanc opened this issue Jun 29, 2021 · 5 comments
Assignees
Labels
kind/bug-report Something doesn't seem to work.

Comments

@Strilanc
Copy link
Contributor

Strilanc commented Jun 29, 2021

Ran into this:

import cirq
op = cirq.GateOperation(
    gate=cirq.MeasurementGate(
        1, cirq.MeasurementKey(path=(), name='q0_1_0', qubits=()), ()
    ),
    qubits=[cirq.GridQubit(0, 1)]
)
assert op == eval(repr(op))  # fails

It took me an hour to debug that it was the repr lying to me. Very frustrating. Either the repr needs to correctly represent the mkey qubits differing from the operation qubits, or the creation of the object needs to fail.

I'm also worried about the fact that we apparently introduced a whole new way for things to be inconsistent, by allowing the measurement key to mention qubits.

It looks like the issue is that MeasurementGate.on rekeys the measurement key to target certain qubits, but that's not the only way to apply a measurement gate to qubits, so the rekeying is inconsistent between various code paths.

@Strilanc Strilanc added the kind/bug-report Something doesn't seem to work. label Jun 29, 2021
@95-martin-orion
Copy link
Collaborator

@smitsanghavi can you take a look at this? Related: #4040

@Strilanc
Copy link
Contributor Author

I'll also note that forcing the key to change as part of putting it into a measurement is not ideal because it means certain circuit rewrites become impossible. What if I want to rewrite the circuit in a way that moves the measurement to other qubits, but I want to preserve the key for compatibility with the original circuit?

@95-martin-orion
Copy link
Collaborator

Given that @smitsanghavi is currently out of office and it may take some time to come to a good solution on this, I recommend we revert the latest PR in the MeasurementKey sequence and work forwards from there.

@smitsanghavi
Copy link
Collaborator

I agree this is an issue. I think it stems from the fact that in direct construction of GateOperation we do not call on on the gate (unlike in cirq.measure) and so the qubits are not passed to the MeasurementKey but op_repr assumes that qubits have been passed.

I'll take a closer look at this (and possibly reevaluate the requirements) when I am back but reverting sounds perfect meanwhile.

@95-martin-orion
Copy link
Collaborator

#4277 fixes this by reverting the addition of the qubit field to MeasurementKey. Prior to that change, MeasurementKey was a simple wrapper around string keys which did not experience this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug-report Something doesn't seem to work.
Projects
None yet
Development

No branches or pull requests

3 participants