diff --git a/qlasskit/bqm.py b/qlasskit/bqm.py index d5fb4b39..26faa375 100644 --- a/qlasskit/bqm.py +++ b/qlasskit/bqm.py @@ -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() @@ -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(): \ No newline at end of file +def decode_samples(qf, sampleset): + model = qf.to_bqm('pq_model') + return model.decode_sampleset(sampleset) \ No newline at end of file diff --git a/test/test_qlassf_to_bqm.py b/test/test_qlassf_to_bqm.py index 4b78ce8b..509733c5 100644 --- a/test/test_qlassf_to_bqm.py +++ b/test/test_qlassf_to_bqm.py @@ -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) @@ -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):