Skip to content

Commit

Permalink
Compile any QuantumWorld to qubits (#76)
Browse files Browse the repository at this point in the history
* Add the option to compile the quantum world to qubits.

* Unify ancilla and compiled qubits logic. Add tests.

* More tests

* Reformat with black

* TicTacToe, more tests and reformatting

* Refactor

* Address comments, add more tests

* Support qudit operations in SparseSimulator via compilation

* Add comment for compiled_qubits dict

* Appease lint checker for Windows
  • Loading branch information
smitsanghavi authored Feb 1, 2023
1 parent 9a345df commit 2232102
Show file tree
Hide file tree
Showing 14 changed files with 635 additions and 133 deletions.
2 changes: 1 addition & 1 deletion unitary/alpha/quantum_effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _verify_objects(self, *objects):
q.num_states != required_dimension
):
raise ValueError(
f"Cannot apply effect to qids of dimension {required_dimension}."
f"Cannot apply effect to qids of dimension {q.num_states}."
)
if q.world is None:
raise ValueError(
Expand Down
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

0 comments on commit 2232102

Please sign in to comment.