Skip to content

Commit

Permalink
class for quantum circuit abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 3, 2023
1 parent 33f43f6 commit 772ee76
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 103 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [x] Ast2logic: handle multiple result
- [x] Ast2logic: fix ret_type for multiple results
- [x] QlassF: truth table creation
- [x] Quantum circuit abstraction
- [ ] Extend testing to compilation
- [ ] Int arithmetic expressions
- [ ] Compiler: prepare invertible abstraction
Expand Down
2 changes: 2 additions & 0 deletions qlasskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# isort:skip_file

__version__ = "0.0.1"

from . import exceptions # noqa: F401
from .qcircuit import QCircuit # noqa: F401
from .qlassf import QlassF, qlassf # noqa: F401
from .typing import Qint2, Qint4, Qint8, Qint12, Qint16, Qtype # noqa: F401
4 changes: 3 additions & 1 deletion qlasskit/compiler/4_invertible_to_qcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from . import ILogic, QCircuit

from .. import QCircuit
from . import ILogic


def translate_invertible(icircuit: ILogic) -> QCircuit:
Expand Down
4 changes: 3 additions & 1 deletion qlasskit/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# isort:skip_file


from .compiler import to_quantum # noqa: F401
from .invertible import ILogic # noqa: F401
from .qcircuit import QCircuit # noqa: F401
88 changes: 33 additions & 55 deletions qlasskit/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Tuple

from sympy import Symbol, simplify, symbols
from sympy.logic import ITE, And, Implies, Not, Or, boolalg
from sympy.logic.boolalg import Boolean

from ..typing import BoolExpList
from .. import QCircuit
from ..typing import Args, BoolExpList


class CompilerException(Exception):
pass


class CompilerResult:
def __init__(self, res_qubit, gate_list, qubit_map):
self.res_qubit = res_qubit
self.gate_list = gate_list
self.qubit_map = qubit_map

@property
def num_qubits(self):
return len(self.qubit_map)

def to_qiskit(self):
from qiskit import QuantumCircuit

qc = QuantumCircuit(len(self.qubit_map), 0)

for g in self.gate_list:
# match g[0]:
if g[0] == "x":
qc.x(g[1])
elif g[0] == "cx":
qc.cx(g[1], g[2])
elif g[0] == "ccx":
qc.ccx(g[1], g[2], g[3])

return qc.to_gate()


class Compiler:
def __init__(self):
self.qmap = {}
Expand All @@ -67,7 +43,7 @@ def _symplify_exp(self, exp):
exp = boolalg.to_cnf(exp)
return exp

def compile(self, expr):
def compile(self, args: Args, ret_size: int, expr: BoolExpList) -> QCircuit:
raise Exception("abstract")


Expand All @@ -78,61 +54,63 @@ class MultipassCompiler(Compiler):
class POCCompiler(Compiler):
"""POC compiler translating an expression list to quantum circuit"""

def compile(self, exprs: BoolExpList):
def compile(self, args: Args, ret_size: int, exprs: BoolExpList) -> QCircuit:
qc = QCircuit()
self.qmap = {}
gl = []

for sym, exp in exprs:
iret, gates = self.compile_expr(self._symplify_exp(exp))
gl.extend(gates)
iret, qc = self.compile_expr(qc, self._symplify_exp(exp))
self.qmap[sym] = iret
if sym == Symbol("_ret"): # TODO: this won't work with multiple res
return iret, gl

def compile_expr(self, expr: Boolean): # noqa: C901
return qc

def compile_expr(
self, qc: QCircuit, expr: Boolean
) -> Tuple[int, QCircuit]: # noqa: C901
# match expr:
if isinstance(expr, Symbol):
if expr.name not in self.qmap:
self.qmap[expr.name] = len(self.qmap)
return self.qmap[expr.name], []
qc.add_qubit()
return self.qmap[expr.name], qc

elif isinstance(expr, Not):
i, g = self.compile_expr(expr.args[0])
return i, g + [("x", i)]
i, qc = self.compile_expr(qc, expr.args[0])
qc.x(i)
return i, qc

elif isinstance(expr, And):
il = []
gl = []

for x in expr.args:
ii, gg = self.compile_expr(x)
ii, qc = self.compile_expr(qc, x)
il.append(ii)
gl.extend(gg)

iold = il[0]
for x in range(1, len(il)):
inew = len(self.qmap)
qc.add_qubit()
self.qmap[f"anc_{len(self.qmap)}"] = inew
gl.append(("ccx", iold, il[x], inew))
qc.ccx(iold, il[x], inew)
iold = inew

return inew, gl
return inew, qc

elif isinstance(expr, Or):
if len(expr.args) > 2:
raise Exception("too many clause")

i1, g1 = self.compile_expr(expr.args[0])
i2, g2 = self.compile_expr(expr.args[1])
i1, qc = self.compile_expr(qc, expr.args[0])
i2, qc = self.compile_expr(qc, expr.args[1])
i3 = len(self.qmap)
qc.add_qubit()
self.qmap[f"anc_{len(self.qmap)}"] = i3

return i3, g1 + g2 + [
("x", i2),
("ccx", i1, i2, i3),
("x", i2),
("cx", i2, i3),
]
qc.x(i2)
qc.ccx(i1, i2, i3)
qc.x(i2)
qc.cx(i2, i3)
return i3, qc

elif isinstance(expr, boolalg.BooleanFalse) or isinstance(
expr, boolalg.BooleanTrue
Expand All @@ -143,11 +121,11 @@ def compile_expr(self, expr: Boolean): # noqa: C901
raise Exception(expr)


def to_quantum(exprs, compiler="poc"):
def to_quantum(args, ret_size, exprs, compiler="poc"):
if compiler == "multipass":
s = MultipassCompiler()
elif compiler == "poc":
s = POCCompiler()

res_qubit, gate_list = s.compile(exprs)
return CompilerResult(res_qubit, gate_list, s.qmap)
circ = s.compile(args, ret_size, exprs)
return circ
44 changes: 0 additions & 44 deletions qlasskit/compiler/qcircuit.py

This file was deleted.

Loading

0 comments on commit 772ee76

Please sign in to comment.