Skip to content

Commit

Permalink
add sampling to bqm testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Feb 13, 2024
1 parent b62d27f commit c477c62
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
7 changes: 4 additions & 3 deletions qlasskit/bqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def to_bqm(args, returns, exprs, fmt: BQMFormat):
# e = AndConst(al[0], And(Not(al[1]), And(al[2], Not(al[3]))), Binary("_ret"), '_ret')

model = e.compile()
print(e)
# print(e)

if fmt == "bqm":
return model.to_bqm()
Expand All @@ -98,5 +98,6 @@ def to_bqm(args, returns, exprs, fmt: BQMFormat):
raise Exception(f"Unknown format `{fmt}")


# TODO: utility for samples decoding
# def decode_samples():
def decode_samples(qf, sampleset):
model = qf.to_bqm('pq_model')
return model.decode_sampleset(sampleset)
28 changes: 26 additions & 2 deletions test/test_qlassf_to_bqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
import unittest

from qlasskit import Qint2, Qint4, QlassF, exceptions, qlassf
import neal


def sample_bqm(bqm, reads=10):
sa = neal.SimulatedAnnealingSampler()
sampleset = sa.sample(bqm, num_reads=reads)
return sampleset
# best_sample = min(decoded_samples, key=lambda x: x.energy)



Expand All @@ -23,19 +31,35 @@ def test_to_bqm_1(self):
f = "def test(a: bool) -> bool:\n\treturn not a"
qf = qlassf(f, to_compile=False)
bqm = qf.to_bqm()
ss = sample_bqm(bqm)
ds = qf.to_bqm('pq_model').decode_sampleset(ss)
print(ss, ds)

def test_to_bqm_2(self):
f = "def test(a: Qint2) -> bool:\n\treturn a == 2"
f = "def test(a: Qint2) -> bool:\n\treturn a != 2"
qf = qlassf(f, to_compile=False)
bqm = qf.to_bqm()
print(bqm)
ss = sample_bqm(bqm)
ds = qf.to_bqm('pq_model').decode_sampleset(ss)
print(ss, ds)

def test_to_bqm_3(self):
f = "def test(a: Qint2) -> Qint2:\n\treturn a + 1"
qf = qlassf(f, to_compile=False)
bqm = qf.to_bqm()
ss = sample_bqm(bqm)
ds = qf.to_bqm('pq_model').decode_sampleset(ss)
print(ss, ds)

def test_to_bqm_4(self):
f = "def test(a: Qint4) -> Qint4:\n\treturn a + 2"
qf = qlassf(f, to_compile=False)
bqm = qf.to_bqm()
ss = sample_bqm(bqm)
ds = qf.to_bqm('pq_model').decode_sampleset(ss)
print(ss, ds)



# class TestQlassfToBQMSamples(unittest.TestCase):
# def test_to_bqm_samples_1(self):

0 comments on commit c477c62

Please sign in to comment.