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

Compile any QuantumWorld to qubits #76

Merged
merged 10 commits into from
Feb 1, 2023
20 changes: 12 additions & 8 deletions unitary/alpha/quantum_effect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
Q1 = cirq.NamedQubit("q1")


@pytest.mark.parametrize("compile_to_qubits", [False, True])
@pytest.mark.parametrize("simulator", [cirq.Simulator, SparseSimulator])
def test_quantum_if(simulator):
board = alpha.QuantumWorld(sampler=simulator())
def test_quantum_if(simulator, compile_to_qubits):
board = alpha.QuantumWorld(sampler=simulator(), compile_to_qubits=compile_to_qubits)
piece = alpha.QuantumObject("q0", 1)
piece2 = alpha.QuantumObject("q1", 0)
board.add_object(piece)
Expand All @@ -44,9 +45,10 @@ def test_quantum_if(simulator):
assert (result[1] == 1 for result in results)


@pytest.mark.parametrize("compile_to_qubits", [False, True])
@pytest.mark.parametrize("simulator", [cirq.Simulator, SparseSimulator])
def test_anti_control(simulator):
board = alpha.QuantumWorld(sampler=simulator())
def test_anti_control(simulator, compile_to_qubits):
board = alpha.QuantumWorld(sampler=simulator(), compile_to_qubits=compile_to_qubits)
piece = alpha.QuantumObject("q0", 0)
piece2 = alpha.QuantumObject("q1", 0)
board.add_object(piece)
Expand All @@ -72,8 +74,9 @@ def test_no_world():
alpha.Flip()(piece)


def test_bad_length():
board = alpha.QuantumWorld()
@pytest.mark.parametrize("compile_to_qubits", [False, True])
def test_bad_length(compile_to_qubits):
board = alpha.QuantumWorld(compile_to_qubits=compile_to_qubits)
piece = alpha.QuantumObject("q0", 1)
board.add_object(piece)
with pytest.raises(ValueError, match="Not able to equate"):
Expand All @@ -83,8 +86,9 @@ def test_bad_length():
alpha.Split()(piece)


def test_no_qutrits():
board = alpha.QuantumWorld()
@pytest.mark.parametrize("compile_to_qubits", [False, True])
def test_no_qutrits(compile_to_qubits):
board = alpha.QuantumWorld(compile_to_qubits=compile_to_qubits)
piece = alpha.QuantumObject("q0", 2)
board.add_object(piece)
with pytest.raises(ValueError, match="Cannot apply effect to qids"):
Expand Down
24 changes: 16 additions & 8 deletions unitary/alpha/quantum_object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
from unitary.alpha.sparse_vector_simulator import SparseSimulator


@pytest.mark.parametrize("compile_to_qubits", [False, True])
@pytest.mark.parametrize("simulator", [cirq.Simulator, SparseSimulator])
def test_negation(simulator):
def test_negation(simulator, compile_to_qubits):
piece = alpha.QuantumObject("t", 0)
board = alpha.QuantumWorld(piece, sampler=simulator())
board = alpha.QuantumWorld(
piece, sampler=simulator(), compile_to_qubits=compile_to_qubits
)
assert board.peek() == [[0]]
-piece
assert board.peek() == [[1]]
Expand All @@ -34,17 +37,21 @@ def test_negation(simulator):
assert board.peek() == [[1]]


@pytest.mark.parametrize("compile_to_qubits", [False, True])
@pytest.mark.parametrize("simulator", [cirq.Simulator, SparseSimulator])
def test_add_world_after_state_change(simulator):
def test_add_world_after_state_change(simulator, compile_to_qubits):
piece = alpha.QuantumObject("t", 0)
piece += 1
board = alpha.QuantumWorld(piece, sampler=simulator())
board = alpha.QuantumWorld(
piece, sampler=simulator(), compile_to_qubits=compile_to_qubits
)
assert board.peek() == [[1]]


def test_qutrit():
@pytest.mark.parametrize("compile_to_qubits", [False, True])
def test_qutrit(compile_to_qubits):
piece = alpha.QuantumObject("t", 2)
board = alpha.QuantumWorld(piece)
board = alpha.QuantumWorld(piece, compile_to_qubits=compile_to_qubits)
assert board.peek() == [[2]]
piece += 1
assert board.peek() == [[0]]
Expand All @@ -58,14 +65,15 @@ def test_qutrit():
assert board.peek() == [[2]]


def test_enum():
@pytest.mark.parametrize("compile_to_qubits", [False, True])
def test_enum(compile_to_qubits):
class Color(enum.Enum):
RED = 0
YELLOW = 1
GREEN = 2

piece = alpha.QuantumObject("t", Color.YELLOW)
board = alpha.QuantumWorld(piece)
board = alpha.QuantumWorld(piece, compile_to_qubits=compile_to_qubits)
assert board.peek() == [[Color.YELLOW]]
piece += Color.YELLOW
assert board.peek() == [[Color.GREEN]]
Expand Down
Loading