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] Add Undo functionality #169

Closed
wants to merge 27 commits into from
7 changes: 6 additions & 1 deletion unitary/alpha/quantum_effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ def __call__(self, *objects):
"""Apply the Quantum Effect to the objects."""
self._verify_objects(*objects)
world = objects[0].world
world.add_effect(list(self.effect(*objects)))
effects = list(self.effect(*objects))
if len(effects) > 0:
print("### call of QuantumEffect")
print(effects)
world.add_effect(effects)
print("\n\n")

def __str__(self):
return self.__class__.__name__
Expand Down
39 changes: 39 additions & 0 deletions unitary/alpha/quantum_world_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,30 +656,44 @@ def test_get_histogram_and_get_probabilities_one_binary_qobject(
)
histogram = world.get_histogram()
assert histogram == [{0: 0, 1: 100}]
histogram = world.get_histogram_with_all_objects_as_key()
assert histogram == {(1,): 100}
probs = world.get_probabilities()
assert probs == [{0: 0.0, 1: 1.0}]
bin_probs = world.get_binary_probabilities()
assert bin_probs == [1.0]
bin_probs = world.get_binary_probabilities_from_state_vector()
assert bin_probs == [1.0]
alpha.Flip()(l1)
histogram = world.get_histogram()
assert histogram == [{0: 100, 1: 0}]
histogram = world.get_histogram_with_all_objects_as_key()
assert histogram == {(0,): 100}
probs = world.get_probabilities()
assert probs == [{0: 1.0, 1: 0.0}]
bin_probs = world.get_binary_probabilities()
assert bin_probs == [0.0]
bin_probs = world.get_binary_probabilities_from_state_vector()
assert bin_probs == [0.0]
alpha.Superposition()(l1)
histogram = world.get_histogram()
assert len(histogram) == 1
assert len(histogram[0]) == 2
assert histogram[0][0] > 10
assert histogram[0][1] > 10
histogram = world.get_histogram_with_all_objects_as_key()
assert len(histogram) == 2
assert histogram[(0,)] > 10
assert histogram[(1,)] > 10
probs = world.get_probabilities()
assert len(probs) == 1
assert len(probs[0]) == 2
assert probs[0][0] > 0.1
assert probs[0][1] > 0.1
bin_probs = world.get_binary_probabilities()
assert 0.1 <= bin_probs[0] <= 1.0
bin_probs = world.get_binary_probabilities_from_state_vector()
assert 0.1 <= bin_probs[0] <= 1.0


@pytest.mark.parametrize(
Expand All @@ -700,12 +714,21 @@ def test_get_histogram_and_get_probabilities_one_trinary_qobject(
)
histogram = world.get_histogram()
assert histogram == [{0: 0, 1: 100, 2: 0}]
histogram = world.get_histogram_with_all_objects_as_key()
assert histogram == {(1,): 100}
probs = world.get_probabilities()
assert probs == [{0: 0.0, 1: 1.0, 2: 0.0}]
bin_probs = world.get_binary_probabilities()
assert bin_probs == [1.0]


def test_get_binary_probabilities_from_state_vector_one_trinary_qobject():
l1 = alpha.QuantumObject("l1", StopLight.YELLOW)
world = alpha.QuantumWorld([l1], sampler=cirq.Simulator(), compile_to_qubits=False)
bin_probs = world.get_binary_probabilities_from_state_vector()
assert bin_probs == [1.0]


@pytest.mark.parametrize(
("simulator", "compile_to_qubits"),
[
Expand All @@ -723,13 +746,29 @@ def test_get_histogram_and_get_probabilities_two_qobjects(simulator, compile_to_
)
histogram = world.get_histogram()
assert histogram == [{0: 0, 1: 100}, {0: 0, 1: 100, 2: 0}]
histogram = world.get_histogram_with_all_objects_as_key()
assert histogram == {(1, 1): 100}
probs = world.get_probabilities()
assert probs == [{0: 0.0, 1: 1.0}, {0: 0.0, 1: 1.0, 2: 0.0}]
bin_probs = world.get_binary_probabilities()
assert bin_probs == [1.0, 1.0]
histogram = world.get_histogram(objects=[l2], count=1000)
assert histogram == [{0: 0, 1: 1000, 2: 0}]
histogram = world.get_histogram_with_all_objects_as_key(objects=[l2], count=1000)
assert histogram == {(1,): 1000}
probs = world.get_probabilities(objects=[l2], count=1000)
assert probs == [{0: 0.0, 1: 1.0, 2: 0.0}]
bin_probs = world.get_binary_probabilities(objects=[l2], count=1000)
assert bin_probs == [1.0]


def test_get_binary_probabilities_from_state_vector_two_qobjects():
l1 = alpha.QuantumObject("l1", Light.GREEN)
l2 = alpha.QuantumObject("l2", StopLight.GREEN)
world = alpha.QuantumWorld(
[l1, l2], sampler=cirq.Simulator(), compile_to_qubits=False
)
bin_probs = world.get_binary_probabilities_from_state_vector()
assert bin_probs == [1.0, 1.0]
bin_probs = world.get_binary_probabilities_from_state_vector(objects=[l2])
assert bin_probs == [1.0]
2 changes: 2 additions & 0 deletions unitary/alpha/qubit_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ def num_objects(self):
return 3

def effect(self, *objects):
print("## PhasedSplit")
yield cirq.ISWAP(objects[0].qubit, objects[1].qubit) ** 0.5
yield cirq.ISWAP(objects[0].qubit, objects[2].qubit) ** 0.5
yield cirq.ISWAP(objects[0].qubit, objects[2].qubit) ** 0.5
print("## PhasedSplit 1")

def __eq__(self, other):
return isinstance(other, PhasedSplit) or NotImplemented
7 changes: 3 additions & 4 deletions unitary/examples/quantum_chinese_chess/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Copyright 2023 The Unitary Authors
# Copyright 2022 Google
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Package for the quantum Chinese chess game."""
#
10 changes: 5 additions & 5 deletions unitary/examples/quantum_chinese_chess/enums_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_type_of():
assert Type.type_of("P") == Type.PAWN
assert Type.type_of("k") == Type.KING
assert Type.type_of("K") == Type.KING
assert Type.type_of(".") == Type.EMPTY
assert Type.type_of("") == Type.EMPTY
assert Type.type_of("b") == None


Expand All @@ -34,7 +34,7 @@ def test_symbol():
assert Type.symbol(Type.HORSE, Color.RED, Language.ZH) == "马"
assert Type.symbol(Type.HORSE, Color.BLACK, Language.ZH) == "馬"

assert Type.symbol(Type.EMPTY, Color.RED) == "."
assert Type.symbol(Type.EMPTY, Color.BLACK) == "."
assert Type.symbol(Type.EMPTY, Color.RED, Language.ZH) == "."
assert Type.symbol(Type.EMPTY, Color.BLACK, Language.ZH) == "."
assert Type.symbol(Type.EMPTY, Color.RED) == ""
assert Type.symbol(Type.EMPTY, Color.BLACK) == ""
assert Type.symbol(Type.EMPTY, Color.RED, Language.ZH) == ""
assert Type.symbol(Type.EMPTY, Color.BLACK, Language.ZH) == ""
44 changes: 44 additions & 0 deletions unitary/examples/quantum_chinese_chess/piece1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2023 The Unitary Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unitary.alpha import QuantumObject
from unitary.examples.quantum_chinese_chess.enums import (
SquareState,
Language,
Color,
Type,
)


class Piece:
def __init__(self, type_: Type, color: Color):
self.type_ = type_
self.color = color
self.is_entangled = False

def symbol(self, lang: Language = Language.EN) -> str:
return Type.symbol(self.type_, self.color, lang)

def __str__(self):
return self.symbol()

def clear(self):
self.type_ = Type.EMPTY
self.color = Color.NA
self.is_entangled = False


class QuantumPiece(Piece, QuantumObject):
def __init__(self, name: str, state: SquareState, type_: Type, color: Color):
QuantumObject.__init__(self, name, state)
Piece.__init__(self, name, state, type_, color)
6 changes: 3 additions & 3 deletions unitary/examples/quantum_chinese_chess/piece_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def test_symbol():
assert p1.symbol(Language.ZH) == "馬"

p2 = Piece("c2", SquareState.EMPTY, Type.EMPTY, Color.NA)
assert p2.symbol() == "."
assert p2.__str__() == "."
assert p2.symbol(Language.ZH) == "."
assert p2.symbol() == ""
assert p2.__str__() == ""
assert p2.symbol(Language.ZH) == ""


def test_enum():
Expand Down
Loading
Loading