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

[Quantum Chinese Chess] Implement more move types #172

Merged
merged 5 commits into from
Dec 8, 2023

Conversation

madcpf
Copy link
Collaborator

@madcpf madcpf commented Nov 30, 2023

Implement the following types of moves:

  • slide
  • split slide
  • merge slide
  • cannon fire

@madcpf madcpf requested a review from dstrain115 November 30, 2023 19:59
@dstrain115 dstrain115 requested a review from solodov December 1, 2023 22:13
@@ -402,7 +402,18 @@ def apply_move(self, str_to_parse: str) -> None:
Jump(move_variant)(source_0, target_0)
elif move_type == MoveType.JUMP:
Jump(move_variant)(source_0, target_0)
# TODO(): apply other move types.
elif move_type == MoveType.SLIDE:
Copy link
Collaborator

Choose a reason for hiding this comment

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

what version of python is this targetting? With 3.11 this big if/else can be made much nicer with:

match move_type:
    case MoveType.CLASSICAL:
        # code
    case MoveType.JUMP:
        # code

I think we'd also get lint checks of not all enum values are checked.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The dependency is python3.9. Maybe we can change it later when 3.11 is the dependency. Adding a TODO() here. Thanks.

unitary/examples/quantum_chinese_chess/chess.py Outdated Show resolved Hide resolved
def num_objects(self) -> Optional[int]:
return 2

def effect(self, *objects) -> Iterator[cirq.Operation]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any expectation of type of values in *objects? perhaps add typing annotation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This method is derived from QuantumEffect. I've updated the methods in QuantumEffects with typing annotation. Thanks.

return 2

def effect(self, *objects) -> Iterator[cirq.Operation]:
source_0, target_0 = objects
Copy link
Collaborator

Choose a reason for hiding this comment

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

this might be error-prone, the method signature says any number of objects, but this unpacking will break if not exactly two.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, the function signature is from the super class.
However, at the very least, we probably want a better error message if the wrong number of objects are passed in.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a subclass of QuantumEffect, which will run a _verify_objects() method that will verify num_dimension() and num_objects() are satisfied by the objects. Thanks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks Doug. QuantumEffect._verify_objects() returns the following error message now.

    def _verify_objects(self, *objects: "QuantumObject"):
        if self.num_objects() is not None and len(objects) != self.num_objects():
            raise ValueError(f"Cannot apply effect to {len(objects)} qubits.")
...

# TODO(): consider specific cases to save the ancilla.
# Add a new ancilla to indicate whether the fire could be made (value = 1 means it could).
capture_ancilla = world._add_ancilla(
f"{expect_occupied_path_piece.name}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

is expect_occupied_path_piece.name not guaranteed to be a string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes it is a string. It's fixed now. Thanks.

@madcpf madcpf requested a review from solodov December 5, 2023 18:52
Copy link
Collaborator

@dstrain115 dstrain115 left a comment

Choose a reason for hiding this comment

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

This is a pretty long PR, but I think all the logic is correct from my read-through.

Added a few minor comments then LGTM.

@@ -21,7 +21,7 @@
MoveType,
MoveVariant,
)
from unitary.examples.quantum_chinese_chess.move import Jump
from unitary.examples.quantum_chinese_chess.move import *
Copy link
Collaborator

Choose a reason for hiding this comment

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

Importing star is not recommended. In fact, the style guide recommends not even importing individual classes:
go/pystyle#imports

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks Doug. I'm changing away from *. Maybe later I can changing away from importing individual classes across all files.

return 2

def effect(self, *objects) -> Iterator[cirq.Operation]:
source_0, target_0 = objects
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, the function signature is from the super class.
However, at the very least, we probably want a better error message if the wrong number of objects are passed in.

unitary/examples/quantum_chinese_chess/move.py Outdated Show resolved Hide resolved
@madcpf madcpf merged commit e54c205 into quantumlib:main Dec 8, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants