-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""Init file for the validator passes.""" | ||
|
||
from opensquirrel.passes.validator.general_validator import Validator | ||
from opensquirrel.passes.validator.native_gate_validator import NativeGateValidator | ||
|
||
__all__ = ["NativeGateValidator", "Validator"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
from opensquirrel.ir import IR | ||
|
||
|
||
class Validator(ABC): | ||
@abstractmethod | ||
def validate(self, ir: IR) -> None: | ||
"""Base validate method to be implemented by inheriting validator classes.""" | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from opensquirrel.ir import IR, Unitary | ||
from opensquirrel.passes.validator import Validator | ||
|
||
|
||
class NativeGateValidator(Validator): | ||
def __init__(self, native_gate_set: list[str]) -> None: | ||
self.native_gate_set = native_gate_set | ||
|
||
def validate(self, ir: IR) -> None: | ||
""" | ||
Check if all unitary gates in the circuit are part of the native gate set. | ||
Args: | ||
ir (IR): The intermediate representation of the circuit to be checked. | ||
Raises: | ||
ValueError: If any unitary gate in the circuit is not part of the native gate set. | ||
""" | ||
gates_not_in_native_gate_set = [ | ||
statement.name | ||
for statement in ir.statements | ||
if isinstance(statement, Unitary) and statement.name not in self.native_gate_set | ||
] | ||
if gates_not_in_native_gate_set: | ||
error_message = f"The following gates are not in the native gate set: {set(gates_not_in_native_gate_set)}" | ||
raise ValueError(error_message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Tests for native gate checker pass | ||
import pytest | ||
|
||
from opensquirrel import CircuitBuilder | ||
from opensquirrel.circuit import Circuit | ||
from opensquirrel.passes.validator import NativeGateValidator | ||
|
||
|
||
@pytest.fixture(name="validator") | ||
def validator_fixture() -> NativeGateValidator: | ||
native_gate_set = ["I", "X90", "mX90", "Y90", "mY90", "Rz", "CZ"] | ||
return NativeGateValidator(native_gate_set) | ||
|
||
|
||
@pytest.fixture | ||
def circuit_with_matching_gate_set() -> Circuit: | ||
builder = CircuitBuilder(5) | ||
builder.I(0) | ||
builder.X90(1) | ||
builder.mX90(2) | ||
builder.Y90(3) | ||
builder.mY90(4) | ||
builder.Rz(0, 2) | ||
builder.CZ(1, 2) | ||
return builder.to_circuit() | ||
|
||
|
||
@pytest.fixture | ||
def circuit_with_unmatching_gate_set() -> Circuit: | ||
builder = CircuitBuilder(5) | ||
builder.I(0) | ||
builder.X90(1) | ||
builder.mX90(2) | ||
builder.Y90(3) | ||
builder.mY90(4) | ||
builder.Rz(0, 2) | ||
builder.CZ(1, 2) | ||
builder.H(0) | ||
builder.CNOT(1, 2) | ||
return builder.to_circuit() | ||
|
||
|
||
def test_matching_gates(validator: NativeGateValidator, circuit_with_matching_gate_set: Circuit) -> None: | ||
try: | ||
validator.validate(circuit_with_matching_gate_set.ir) | ||
except ValueError: | ||
pytest.fail("validate() raised ValueError unexpectedly") | ||
|
||
|
||
def test_non_matching_gates(validator: NativeGateValidator, circuit_with_unmatching_gate_set: Circuit) -> None: | ||
with pytest.raises(ValueError, match="The following gates are not in the native gate set:.*"): | ||
validator.validate(circuit_with_unmatching_gate_set.ir) |