-
Notifications
You must be signed in to change notification settings - Fork 26
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] Add class Jump(QuantumEffect). #164
Changes from all commits
0e6a382
1b69d6f
8c18f5f
46348d8
bcdb75a
cf35e05
640b685
e8cce5a
576b13c
0ee28c1
40463a1
e3a7fbc
8d5bee5
a69abd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ def from_fen(cls, fen: str = _INITIAL_FEN) -> "Board": | |
FEN rule for Chinese Chess could be found at https://www.wxf-xiangqi.org/images/computer-xiangqi/fen-for-xiangqi-chinese-chess.pdf | ||
""" | ||
chess_board = {} | ||
row_index = 9 | ||
row_index = 0 | ||
king_locations = [] | ||
pieces, turns = fen.split(" ", 1) | ||
for row in pieces.split("/"): | ||
|
@@ -74,15 +74,11 @@ def from_fen(cls, fen: str = _INITIAL_FEN) -> "Board": | |
name, SquareState.OCCUPIED, piece_type, color | ||
) | ||
col += 1 | ||
row_index -= 1 | ||
row_index += 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next time, please separate this into two PRs, since the print change is independent of the board display change. It's fine to keep it here this time though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Doug. I'll send separate changes with concentric topic next time. |
||
board = alpha.QuantumWorld(chess_board.values()) | ||
# Here 0 means the player RED while 1 the player BLACK. | ||
current_player = 0 if "w" in turns else 1 | ||
# TODO(): maybe add check to make sure the input fen itself is correct. | ||
if len(king_locations) != 2: | ||
raise ValueError( | ||
f"We expect two KINGs on the board, but got {len(king_locations)}." | ||
) | ||
return cls(board, current_player, king_locations) | ||
|
||
def __str__(self): | ||
|
@@ -92,7 +88,7 @@ def __str__(self): | |
for col in "abcdefghi": | ||
board_string.append(f" {col}") | ||
board_string.append("\n") | ||
for row in range(num_rows): | ||
for row in range(num_rows - 1, -1, -1): | ||
# Print the row index on the left. | ||
board_string.append(f"{row} ") | ||
for col in "abcdefghi": | ||
|
@@ -165,7 +161,7 @@ def path_pieces(self, source: str, target: str) -> Tuple[List[str], List[str]]: | |
elif abs(dy) == 2 and abs(dx) == 1: | ||
pieces.append(f"{chr(x0)}{y0 + dy_sign}") | ||
else: | ||
raise ValueError("Unexpected input to path_pieces().") | ||
raise ValueError("The input move is illegal.") | ||
for piece in pieces: | ||
if self.board[piece].is_entangled: | ||
quantum_pieces.append(piece) | ||
|
@@ -188,3 +184,15 @@ def flying_general_check(self) -> bool: | |
# If there are no pieces between two KINGs, the check successes. Game ends. | ||
return True | ||
# TODO(): add check when there are quantum pieces in between. | ||
|
||
def sample(self, repetitions: int) -> List[int]: | ||
"""Sample the current board by the given `repetitions`. | ||
Returns a list of 90-bit bitstring, each corresponding to one sample. | ||
""" | ||
samples = self.board.peek(count=repetitions, convert_to_enum=False) | ||
# Convert peek results (in List[List[int]]) into List[int]. | ||
samples = [ | ||
int("0b" + "".join([str(i) for i in sample[::-1]]), base=2) | ||
for sample in samples | ||
] | ||
return samples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this return the ancilla?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method works like reset. (Maybe I should rename it to reset()?) So I don't think user should apply more effects on the ancilla. They should move forward with the reset
object
, which since now has a new reset value.