Skip to content

Commit

Permalink
Tests for NativeInterpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
rmshaffer committed May 14, 2024
1 parent ffb1dac commit fd04643
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
8 changes: 8 additions & 0 deletions test/resources/inputs.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
OPENQASM 3.0;
input float theta;
output bit return_value;
qubit[1] __qubits__;
rx(theta) __qubits__[0];
bit __bit_0__;
__bit_0__ = measure __qubits__[0];
return_value = __bit_0__;
64 changes: 55 additions & 9 deletions test/unit_tests/autoqasm/simulator/test_native_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from autoqasm.simulator.native_interpreter import NativeInterpreter
from autoqasm.simulator.simulation import Simulation

INPUTS_QASM = "test/resources/inputs.qasm"


@pytest.mark.parametrize(
"reset_instructions",
Expand All @@ -28,15 +30,59 @@
)
def test_reset(reset_instructions):
qasm = f"""
OPENQASM 3.0;
qubit[2] __qubits__;
x __qubits__[0];
x __qubits__[1];
{reset_instructions}
bit[2] __bit_0__ = "00";
__bit_0__[0] = measure __qubits__[0];
__bit_0__[1] = measure __qubits__[1];
OPENQASM 3.0;
qubit[2] __qubits__;
x __qubits__[0];
{reset_instructions}
bit[2] __bit_0__ = "00";
__bit_0__[0] = measure __qubits__[0];
__bit_0__[1] = measure __qubits__[1];
"""

result = NativeInterpreter(Simulation(0, 0, 1)).simulate(qasm)
assert result["__bit_0__"] == ["00"]


def test_inputs_outputs():
with open(INPUTS_QASM, encoding="utf-8", mode="r") as f:
qasm = f.read()

result = NativeInterpreter(Simulation(1, 1, 1)).simulate(qasm, inputs={"theta": 0.0})
assert result["return_value"] == [0]


def test_inputs_outputs_from_file():
result = NativeInterpreter(Simulation(1, 1, 1)).simulate(
INPUTS_QASM, inputs={"theta": 0.0}, is_file=True
)
assert result["return_value"] == [0]


def test_missing_input():
qasm = """
OPENQASM 3.0;
input float theta;
"""
with pytest.raises(NameError, match="Missing input variable"):
NativeInterpreter(Simulation(0, 0, 1)).simulate(qasm)


def test_repeated_output_declaration():
qasm = """
OPENQASM 3.0;
output bit return_value;
output bit return_value;
return_value = 0;
"""
result = NativeInterpreter(Simulation(0, 0, 1)).simulate(qasm)
assert result["return_value"] == [0]


def test_qubit_register():
qasm = """
OPENQASM 3.0;
qubit[2] __qubits__;
x __qubits__[1];
bit[2] __bit_0__ = measure __qubits__;
"""
result = NativeInterpreter(Simulation(2, 1, 1)).simulate(qasm)
assert result["__bit_0__"] == ["01"]

0 comments on commit fd04643

Please sign in to comment.