Skip to content

Commit 0bacb4d

Browse files
committed
param parser
1 parent 83998bc commit 0bacb4d

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='abrax',
8-
version='0.2.0',
8+
version='0.2.1',
99
description='A Quantum Circuit DSL',
1010
long_description=long_description,
1111
long_description_content_type='text/markdown',

src/abrax/parser.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import re
1+
from re import compile
22

3-
no_gate=re.compile("-{1,}")
3+
no_gate = compile("-{1,}")
44

55
def parse_circuit(string):
66
lines = map(lambda e: e.strip(), string.strip().split("\n"))
@@ -25,6 +25,14 @@ def parse_circuit(string):
2525

2626
return list(map(list, zip(*by_rows)))
2727

28+
def parse_param(p):
29+
# if float or int continue
30+
if isinstance(p, (float, int)):
31+
return p
32+
# if not, it's a string, if includes . it's a float
33+
p = p.strip()
34+
return float(p) if "." in p else int(p)
35+
2836
def resolve_circuit(circuit, qc, config):
2937
for layerNo, layer in enumerate(circuit):
3038
if all([e.lower() == "-" for e in layer]):
@@ -40,10 +48,14 @@ def resolve_circuit(circuit, qc, config):
4048
gate_name = gate[:gate.index("(")]
4149
param = gate[gate.index("(") + 1:gate.index(")")]
4250
op = getattr(qc, gate_name)
51+
4352
if "," in param:
44-
param = list(map(float, param.split(","))) + [wireNo]
53+
param = param \
54+
.split(",") \
55+
.map(lambda e: parse_param(e))
56+
param = param + [wireNo]
4557
else:
46-
param = [float(param), wireNo]
58+
param = [parse_param(param), wireNo]
4759
else:
4860
param = [wireNo]
4961
op = getattr(qc, gate)

test/index.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from qiskit import QuantumCircuit
22
from abrax import A
3+
from numpy import pi
34

45
qc = QuantumCircuit(5)
5-
circuit = A("""
6+
circuit = A(f"""
67
// I AM JESUS
78
// SPACES ARE SIGNIFICANT, DONOT add them inside a gate
8-
- H CX(4) RX(10)
9-
- H - RY(55)
10-
- H - CX(4)
11-
- H X H
12-
- H - -
9+
- H CX(4) RX({pi})
10+
- H RZ(45) RY({5.5})
11+
- H --- CX(4)
12+
- H X H
13+
- H --- -------
1314
""", qc
1415
)
1516

0 commit comments

Comments
 (0)