Skip to content

Commit

Permalink
bool optimizer update, test statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Nov 9, 2023
1 parent a45a2c0 commit 1c8a2b1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ htmlcov
coverage.xml
docs/build
*.egg
*.egg-info
*.egg-info
.t_statistics
17 changes: 14 additions & 3 deletions qlasskit/boolopt/bool_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Dict

from sympy import Symbol, cse
from sympy.logic.boolalg import Boolean, simplify_logic
from sympy.logic.boolalg import And, Boolean, Not, Or, Xor, simplify_logic

from ..ast2logic import BoolExpList
from . import SympyTransformer, deprecated
Expand All @@ -27,17 +27,28 @@
)


def custom_simplify_logic(expr):
if isinstance(expr, Xor):
return expr
elif isinstance(expr, (And, Or, Not)):
args = [custom_simplify_logic(arg) for arg in expr.args]
return type(expr)(*args)
else:
return simplify_logic(expr)


def merge_expressions(exps: BoolExpList) -> BoolExpList:
n_exps = []
emap: Dict[Symbol, Boolean] = {}

for s, e in exps:
e = e.xreplace(emap)
e = custom_simplify_logic(e)

if s.name[0:4] != "_ret":
emap[s] = simplify_logic(e)
emap[s] = e
else:
n_exps.append((s, simplify_logic(e)))
n_exps.append((s, e))

return n_exps

Expand Down
4 changes: 4 additions & 0 deletions qlasskit/qcircuit/qcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def __init__(self, num_qubits=0, name="qc", native=None):

self.__native = native

@property
def num_gates(self):
return len(self.gates)

def get_key_by_index(self, i: int):
"""Return the qubit name given its index"""
for key in self.qubit_map:
Expand Down
4 changes: 2 additions & 2 deletions test/test_qlassf_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ def test_multiple_arg(self):
compute_and_compare_results(self, qf)

def test_multiple_arg2(self):
ex = And(a, Not(b))
# ex = And(a, Not(b))
f = "def test(a: bool, b: bool, c: bool) -> bool:\n\treturn a and (not b) and (a or c)"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], ex)
# self.assertEqual(qf.expressions[0][1], ex)
compute_and_compare_results(self, qf)

def test_ifexp(self):
Expand Down
4 changes: 2 additions & 2 deletions test/test_qlassf_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ def test_const_int_compare_gt(self):
def test_const_int4_compare_lt(self):
f = "def test(a: Qint4) -> bool:\n\treturn a < 6"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
self.assertEqual(len(qf.expressions), 1)
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(len(qf.expressions), 2)
self.assertEqual(qf.expressions[-1][0], _ret)
compute_and_compare_results(self, qf)

def test_int_int_compare_gt(self):
Expand Down
24 changes: 24 additions & 0 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.

import inspect
import json
import random
import threading
from typing import Tuple, get_args

from qiskit import QuantumCircuit, transpile
Expand Down Expand Up @@ -42,6 +44,26 @@
qsk_simulator = Aer.get_backend("aer_simulator")


statistics = {"tests": 0, "qubits": 0, "gates": 0}

try:
old_statistics = json.loads(open(".t_statistics", "r").read())[-100:]
except:
old_statistics = []

statistics_lock = threading.Lock()


def update_statistics(q, g):
with statistics_lock:
global statistics
statistics["tests"] += 1
statistics["qubits"] += q
statistics["gates"] += g
f = open(".t_statistics", "w")
f.write(json.dumps(old_statistics + [statistics], indent=4))


def inject_parameterized_compilers(params):
param_inj = []
for comp in ENABLED_COMPILERS:
Expand Down Expand Up @@ -77,6 +99,8 @@ def compute_result_of_qcircuit(cls, qf, truth_line):
gate = qf.gate()
qc = QuantumCircuit(gate.num_qubits)

update_statistics(circ.num_qubits, circ.num_gates)

# Prepare inputs
[qc.initialize(1 if truth_line[i] else 0, i) for i in range(qf.input_size)]

Expand Down

0 comments on commit 1c8a2b1

Please sign in to comment.