-
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.
- Implement ControlledSemantic to be able to express CNOT as a controlled X, for instance - Separate validation of qubit ranges from type-checking in a separate ANTLR tree visitor - snake_case instead of camelCase for variable names - Add docstrings to parsing classes - Gates are specified directly with Python functions and a decorator - Lay the basis of handling of anonymous gates - Writer turned into impl pattern - Rename AST -> IR - Visitor pattern for the SquirrelIR class - Use those visitors in e.g. writer and test_interpreter - Various code fixes - Use inspect to know Python function signatures This makes the gate semantic API nice to use from Python.
- Loading branch information
Showing
29 changed files
with
1,060 additions
and
928 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from opensquirrel.circuit import Circuit | ||
from opensquirrel.circuit_builder import CircuitBuilder | ||
from opensquirrel.default_gates import DefaultGates | ||
from opensquirrel.default_gates import default_gate_aliases |
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 |
---|---|---|
@@ -1,32 +1,53 @@ | ||
import inspect | ||
from typing import Callable | ||
|
||
from opensquirrel.circuit import Circuit | ||
from opensquirrel.default_gates import DefaultGates | ||
from opensquirrel.squirrel_ast import SquirrelAST | ||
from opensquirrel.default_gates import default_gate_set, default_gate_aliases | ||
from opensquirrel.gate_library import GateLibrary | ||
from opensquirrel.squirrel_ir import Comment, Qubit, SquirrelIR, Gate | ||
|
||
|
||
class CircuitBuilder: | ||
"""A class using the builder pattern to make construction of circuits easy. | ||
class CircuitBuilder(GateLibrary): | ||
""" | ||
A class using the builder pattern to make construction of circuits easy from Python. | ||
Adds corresponding gate when a method is called. Checks gates are known and called with the right arguments. | ||
Mainly here to allow for Qiskit-style circuit construction: | ||
>>> myCircuit = CircuitBuilder(DefaultGates, 3).h(0).cnot(0, 1).cnot(0, 2).to_circuit() | ||
>>> CircuitBuilder(number_of_qubits=3).h(Qubit(0)).cnot(Qubit(0), Qubit(1)).cnot(Qubit(0), Qubit(2)).to_circuit() | ||
version 3.0 | ||
<BLANKLINE> | ||
qubit[3] q | ||
<BLANKLINE> | ||
h q[0] | ||
cnot q[0], q[1] | ||
cnot q[0], q[2] | ||
<BLANKLINE> | ||
""" | ||
|
||
__default_qubit_register_name = "q" | ||
_default_qubit_register_name = "q" | ||
|
||
def __init__(self, gates: dict, numberOfQubits: int): | ||
self.squirrelAST = SquirrelAST(gates, numberOfQubits, self.__default_qubit_register_name) | ||
def __init__(self, number_of_qubits: int, gate_set: [Callable[..., Gate]] = default_gate_set, gate_aliases: dict[str, Callable[..., Gate]] = default_gate_aliases): | ||
GateLibrary.__init__(self, gate_set, gate_aliases) | ||
self.squirrel_ir = SquirrelIR(number_of_qubits, self._default_qubit_register_name) | ||
|
||
def __getattr__(self, attr): | ||
def addComment(commentString: str): | ||
self.squirrelAST.add_comment(commentString) | ||
def add_comment(comment_string: str): | ||
self.squirrel_ir.add_comment(Comment(comment_string)) | ||
return self | ||
|
||
def addThisGate(*args): | ||
self.squirrelAST.add_gate(attr, *args) | ||
def add_this_gate(*args): | ||
generator_f = GateLibrary.get_gate_f(self, attr) | ||
|
||
for i, par in enumerate(inspect.signature(generator_f).parameters.values()): | ||
if not isinstance(args[i], par.annotation): | ||
raise TypeError( | ||
f"Wrong argument type for gate `{attr}`, got {type(args[i])} but expected {par.annotation}" | ||
) | ||
|
||
self.squirrel_ir.add_gate(generator_f(*args)) | ||
return self | ||
|
||
return addComment if attr == "comment" else addThisGate | ||
return add_comment if attr == "comment" else add_this_gate | ||
|
||
def to_circuit(self) -> Circuit: | ||
return Circuit(self.squirrelAST) | ||
return Circuit(self.squirrel_ir) |
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
Oops, something went wrong.