Skip to content

Commit

Permalink
Add test for the factory module (#15)
Browse files Browse the repository at this point in the history
This adds tests for the module `factory.py`
  • Loading branch information
pimvenderbosch authored Nov 22, 2024
1 parent fb75e71 commit 231968d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Empty file added tests/__init__.py
Empty file.
36 changes: 35 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from __future__ import annotations

from copy import deepcopy
from dataclasses import dataclass
from typing import Any

import pytest

from qadence2_ir.irast import AST
from qadence2_ir.factory import IRBuilder
from qadence2_ir.irast import AST, Attributes
from qadence2_ir.types import Alloc, AllocQubits, Assign, Model, QuInstruct, Support


Expand Down Expand Up @@ -59,3 +62,34 @@ def model_with_directives_settings(simple_model: Model) -> Model:
@pytest.fixture
def asts_for_arithmetic() -> tuple[AST, AST]:
return (AST.numeric(0.5 + 1j), AST.quantum_op("CNOT", (0,), (1,)))


@dataclass
class InputTypeTest:
qubit_count: int
directives: dict[str, Any]
settings: dict[str, Any]
ast: AST


class IRBuilderTest(IRBuilder[InputTypeTest]):
@staticmethod
def set_register(input_obj: InputTypeTest) -> AllocQubits:
return AllocQubits(input_obj.qubit_count)

@staticmethod
def set_directives(input_obj: InputTypeTest) -> Attributes:
return input_obj.directives

@staticmethod
def settings(input_obj: InputTypeTest) -> Attributes:
return input_obj.settings

@staticmethod
def parse_sequence(input_obj: InputTypeTest) -> AST:
return input_obj.ast


@pytest.fixture
def builder() -> IRBuilderTest:
return IRBuilderTest()
24 changes: 24 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

from qadence2_ir import AST, AllocQubits, ir_compiler_factory
from qadence2_ir.types import Alloc, Assign, Call, Load, Model, QuInstruct, Support

from .conftest import InputTypeTest, IRBuilderTest


def test_init(quantum_ast: AST, builder: IRBuilderTest) -> None:
ir_compiler = ir_compiler_factory(builder)
input_ = InputTypeTest(10, {"my-directive": False}, {"my-setting": 8}, quantum_ast)
model = ir_compiler(input_)
expected = Model(
AllocQubits(10),
{"x": Alloc(1, False)},
[
Assign("%0", Call("div", Load("x"), 3)),
Assign("%1", Call("fn", Load("%0"), Load("x"))),
QuInstruct("rx", Support((0,), (1,)), Load("%1")),
],
{"my-directive": False},
{"my-setting": 8},
)
assert model == expected

0 comments on commit 231968d

Please sign in to comment.