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

Add a MeasurementKey class and make it the internal representation in MeasurementGate #4039

Merged
merged 10 commits into from
Apr 27, 2021

Conversation

smitsanghavi
Copy link
Collaborator

@smitsanghavi smitsanghavi commented Apr 21, 2021

@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Apr 21, 2021
@smitsanghavi smitsanghavi marked this pull request as ready for review April 21, 2021 02:57
@smitsanghavi smitsanghavi requested review from cduck, vtomole and a team as code owners April 21, 2021 02:57
@smitsanghavi
Copy link
Collaborator Author

@95-martin-orion (since I can't manually add reviewers)

@95-martin-orion 95-martin-orion requested review from 95-martin-orion and removed request for viathor April 22, 2021 19:28
def key(self, key_str: str):
# TODO: Allow nested keys only when created via some special methods. Planned in Phase 2a
# of https://tinyurl.com/structured-measurement-keys#heading=h.zafcj653k11m
self.mkey = value.MeasurementKey(key_str, allow_nested_key=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make more sense to set allow_nested_key to False until the full logic is in place? Otherwise we could break stuff in between.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm setting it to True to ensure nothing breaks. Currently, a decomposed CircuitOperation will has these nested keys inside regular MeasurementGates (nested keys are just keys with a : inside).

Until Phase 2a of the RFC, the MeasurementKey and MeasurementGate classes will not know if their keys are being remapped in a way that they need to allow nested keys. So setting a blanket allow to ensure nothing triggers the ValueError (except if someone creates a nested MeasurementKey directly).

cirq-core/cirq/ops/measurement_gate.py Outdated Show resolved Hide resolved
def __str__(self):
return self.name

def __hash__(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore this comment if we really want to exclude allow_nested_key from the hash.

This and the _hash field are unnecessary: since both eq and frozen are true for this class, a hash function will be auto-generated: dataclass docs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I defined them specifically to exclude allow_nested_key. I feel it should just be a InitVar but had to keep it around for serialization. More on it in the comment below.


def __hash__(self):
if self._hash is None:
object.__setattr__(self, '_hash', hash(self.name))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: include allow_nested_key in the hash?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should. WDYT?

cirq-core/cirq/value/measurement_key.py Outdated Show resolved Hide resolved
Comment on lines +23 to +24
mkey = cirq.MeasurementKey('')
assert mkey.name == ''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have a class, we might as well prevent empty keys - they can only generate confusion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. A bunch of tests failed since sometimes we don't bother naming the measurements in tests. I didn't want to clutter this PR with all that changes so will send that in a subsequent PR as soon as this is in.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't name the measurement doesn't it give it an automatic name of str(qubits)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cirq.measure does. But cirq.MeasurementGate, being a gate, does not know what qubits it's going to be operating on. So the tests which directly use the gate with on would fail and need explicit keys.

As part of special treatment for qubits (phase 2b of the RFC), MeasurementGate.on would be overridden to pass the qubit info to MeasurementKey which would do remapping and maybe create the default qubit-based key, if needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me - we can handle extra validation in subsequent PRs.

cirq-core/cirq/value/measurement_key.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline - plan is to remove validation from this PR and cover it in subsequent PRs.

Comment on lines +23 to +24
mkey = cirq.MeasurementKey('')
assert mkey.name == ''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me - we can handle extra validation in subsequent PRs.

@95-martin-orion 95-martin-orion added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Apr 27, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Apr 27, 2021
@CirqBot CirqBot merged commit ebce56c into quantumlib:master Apr 27, 2021
@CirqBot CirqBot removed the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Apr 27, 2021
@CirqBot CirqBot removed the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Apr 27, 2021
@smitsanghavi smitsanghavi deleted the mkey branch April 27, 2021 18:57
@@ -0,0 +1 @@
[cirq.MeasurementKey('key'), cirq.MeasurementKey('nested:key')]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this generated by actually calling repr(g) for some object g ?

The _repr_ above returns f'cirq.MeasurementKey(name={self.name})', which seems wrong. See #4440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants