Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
madcpf committed May 13, 2024
1 parent b55e24f commit a71bc4c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 48 deletions.
2 changes: 0 additions & 2 deletions unitary/alpha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@
from unitary.alpha.qudit_effects import (
Cycle,
Flip,
Superpose,
QuditCycle,
QuditFlip,
QuditSuperpose,
)

from unitary.alpha.quantum_object import (
Expand Down
2 changes: 1 addition & 1 deletion unitary/alpha/quantum_effect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ def test_no_qutrits(compile_to_qubits):
piece = alpha.QuantumObject("q0", 2)
board.add_object(piece)
with pytest.raises(ValueError, match="Cannot apply effect to qids"):
alpha.Superposition()(piece)
alpha.Phase()(piece)
6 changes: 3 additions & 3 deletions unitary/alpha/quantum_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def copy(self) -> "QuantumWorld":
for remap in self.qubit_remapping_dict:
new_dict = {}
for key_obj, value_obj in remap.items():
new_dict[new_world.get_object_by_name(key_obj.name)] = (
new_world.get_object_by_name(value_obj.name)
)
new_dict[
new_world.get_object_by_name(key_obj.name)
] = new_world.get_object_by_name(value_obj.name)
new_world.qubit_remapping_dict.append(new_dict)
new_world.qubit_remapping_dict_length = self.qubit_remapping_dict_length.copy()
return new_world
Expand Down
13 changes: 8 additions & 5 deletions unitary/alpha/qubit_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import cirq

from unitary.alpha.quantum_effect import QuantumEffect
from unitary.alpha.qudit_gates import QuditHadamardGate


class Phase(QuantumEffect):
Expand Down Expand Up @@ -67,14 +68,16 @@ def __eq__(self, other):


class Superposition(QuantumEffect):
"""Takes a qubit in a basis state into a superposition."""

def num_dimension(self) -> Optional[int]:
return 2
"""Transforms a qubit (or qudit) in a basis state into a (equal, in terms of
absolute magnitude) superposition of all basis states.
"""

def effect(self, *objects):
for q in objects:
yield cirq.H(q.qubit)
if q.qubit.dimension == 2:
yield cirq.H(q.qubit)
else:
yield QuditHadamardGate(dimension=q.qubit.dimension)(q.qubit)

def __eq__(self, other):
return isinstance(other, Superposition) or NotImplemented
Expand Down
21 changes: 19 additions & 2 deletions unitary/alpha/qubit_effects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

import enum
import pytest

import cirq

import unitary.alpha as alpha


class StopLight(enum.Enum):
RED = 0
YELLOW = 1
GREEN = 2


@pytest.mark.parametrize("compile_to_qubits", [False, True])
@pytest.mark.parametrize("simulator", [cirq.Simulator, alpha.SparseSimulator])
def test_flip(simulator, compile_to_qubits):
Expand Down Expand Up @@ -78,7 +84,7 @@ def test_partial_phase(simulator, compile_to_qubits):

@pytest.mark.parametrize("compile_to_qubits", [False, True])
@pytest.mark.parametrize("simulator", [cirq.Simulator, alpha.SparseSimulator])
def test_superposition(simulator, compile_to_qubits):
def test_qubit_superposition(simulator, compile_to_qubits):
board = alpha.QuantumWorld(sampler=simulator(), compile_to_qubits=compile_to_qubits)
piece = alpha.QuantumObject("t", 0)
board.add_object(piece)
Expand All @@ -87,6 +93,17 @@ def test_superposition(simulator, compile_to_qubits):
assert str(alpha.Superposition()) == "Superposition"


def test_qudit_superposition():
board = alpha.QuantumWorld(sampler=cirq.Simulator(), compile_to_qubits=False)
piece = alpha.QuantumObject("t", StopLight.GREEN)
board.add_object(piece)
alpha.Superposition()(piece)
results = board.peek([piece], count=100)
assert any(result == [StopLight.RED] for result in results)
assert any(result == [StopLight.YELLOW] for result in results)
assert any(result == [StopLight.GREEN] for result in results)


@pytest.mark.parametrize("compile_to_qubits", [False, True])
@pytest.mark.parametrize("simulator", [cirq.Simulator, alpha.SparseSimulator])
def test_move(simulator, compile_to_qubits):
Expand Down
24 changes: 0 additions & 24 deletions unitary/alpha/qudit_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,3 @@ class QuditFlip(Flip):

def __init__(self, dimension: int, state0: int, state1: int):
super().__init__(state0=state0, state1=state1)


class Superpose(QuantumEffect):
"""Transforms each pure state to a (equal, in terms of absolute magnitude) superposition of
all pure states.
"""

def effect(self, *objects):
for q in objects:
if q.qubit.dimension == 2:
yield cirq.H(q.qubit)
else:
yield QuditHadamardGate(dimension=q.qubit.dimension)(q.qubit)


class QuditSuperpose(Superpose):
"""Equivalent to Superpose.
Exists only for backwards compatibiltity.
Will be removed in 2024.
"""

def __init__(self):
super().__init__()
11 changes: 0 additions & 11 deletions unitary/alpha/qudit_effects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,3 @@ def test_qudit_flip(simulator, compile_to_qubits):
alpha.QuditFlip(3, 0, 1)(piece)
results = board.peek([piece], count=100)
assert all(result == [StopLight.GREEN] for result in results)


def test_qudit_superpose():
board = alpha.QuantumWorld(sampler=cirq.Simulator(), compile_to_qubits=False)
piece = alpha.QuantumObject("t", StopLight.GREEN)
board.add_object(piece)
alpha.QuditSuperpose()(piece)
results = board.peek([piece], count=100)
assert any(result == [StopLight.RED] for result in results)
assert any(result == [StopLight.YELLOW] for result in results)
assert any(result == [StopLight.GREEN] for result in results)

0 comments on commit a71bc4c

Please sign in to comment.