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

Change GridDeviceMetadata qubit type to GridQubit #5633

Merged
merged 6 commits into from
Jun 30, 2022

Conversation

verult
Copy link
Collaborator

@verult verult commented Jun 28, 2022

Proposal to change qubit types in GridDeviceMetadata to GridQubit, per @maffoo 's and @mpharrigan 's suggestion.

Looks like GridQid is not a superclass of GridQubit.

Also included a quick docstring fix to disambiguate from cirq_google.GridDevice.

@MichaelBroughton WDYT?

@CirqBot CirqBot added the size: S 10< lines changed <50 label Jun 28, 2022
@verult verult requested a review from MichaelBroughton June 28, 2022 00:07
@MichaelBroughton
Copy link
Collaborator

Seems like a good idea to me!

Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

do you want to remove the casts that someone linked to in our chat in a recent @MichaelBroughton pr?

@verult
Copy link
Collaborator Author

verult commented Jun 29, 2022

@mpharrigan Good point, will fix all the casts used in the codebase.

@verult verult marked this pull request as ready for review June 29, 2022 00:57
@verult verult requested review from wcourtney, a team, vtomole and cduck as code owners June 29, 2022 00:57
@verult
Copy link
Collaborator Author

verult commented Jun 29, 2022

SerializableDevice technically supports both GridQubits and NamedQubits. Since it's being deprecated and all Cirq call sites of SerializableDevice has already been rerouted, I did the simplest thing of breaking qubit types other than GridQubits. Let me know if there are any breakages in downstream dependencies and I'm happy to help fix them.

@verult verult force-pushed the griddevicemetadata-gridqid branch from 6dc35a7 to 4ff386e Compare June 29, 2022 01:03
Returns:
Frozenset of qubits on device.
"""
return cast(FrozenSet['cirq.GridQubit'], super().qubit_set)
Copy link
Collaborator

Choose a reason for hiding this comment

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

wait, why do we need a cast here? Is it because you want to call super() instead of returning frozenset(self.qubits)?

Copy link
Collaborator Author

@verult verult Jun 29, 2022

Choose a reason for hiding this comment

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

Right. self.qubits doesn't exist, and I wasn't sure about adding a self._qubits attribute just for this when it's already accessible from the superclass but just with a more general type.

edit: I tried it without the cast and actually mypy didn't complain locally. Will push it and see if it passes presubmits.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope that didn't work, reverting back

if gate_def.number_of_qubits == 2
for pair in gate_def.target_set
if len(pair) == 2
and pair[0] < pair[1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know this was already here, but does this mean if you order your qubits "backwards" in target_set the metadata will report that there are no pairs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah good catch. This was added when GridDeviceMetadata required the pair to be ordered but missed this call site when it was changed to be order-agnostic. Will fix separately.

Comment on lines +116 to +117
and isinstance(pair[0], cirq.GridQubit)
and isinstance(pair[1], cirq.GridQubit)
Copy link
Collaborator

Choose a reason for hiding this comment

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

doesn't this obviate the need for the cast? Usually mypy can use if isinstance(...) checks to infer variable types

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 was hoping for that too, but unfortunately mypy complained (that the type is List[Tuple[Qid, Qid]])

qubit_pairs=cast(
List[Tuple[cirq.GridQubit, cirq.GridQubit]],
[
(pair[0], pair[1])
Copy link
Collaborator

Choose a reason for hiding this comment

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

put the cast here so you're casting less things? Like it still can infer the List[Tuple[... part; you need to cast the qubits themselves. Although with the isinstance checks I'm sad that that's necessary, see below.

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 tried that at first, but mypy still complained (don't remember the exact error message, though)

@@ -304,7 +304,7 @@ def place_line(self, device: 'cirq_google.GridDevice', length: int) -> GridQubit
if not device.metadata.qubit_set:
return GridQubitLineTuple()

start: GridQubit = cast(GridQubit, min(device.metadata.qubit_set))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice

@verult verult added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 29, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jun 29, 2022
@CirqBot CirqBot merged commit 9c58cd4 into quantumlib:master Jun 30, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jun 30, 2022
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Proposal to change qubit types in `GridDeviceMetadata` to `GridQubit`, per @maffoo 's and @mpharrigan 's suggestion.

Looks like `GridQid` is not a superclass of `GridQubit`.

Also included a quick docstring fix to disambiguate from `cirq_google.GridDevice`.

@MichaelBroughton WDYT?
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Proposal to change qubit types in `GridDeviceMetadata` to `GridQubit`, per @maffoo 's and @mpharrigan 's suggestion.

Looks like `GridQid` is not a superclass of `GridQubit`.

Also included a quick docstring fix to disambiguate from `cirq_google.GridDevice`.

@MichaelBroughton WDYT?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants