Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 0b8b330

Browse files
committed
0.0.4 --> 0.1.0
Revert function previously split into 3 parts into single `factor()`
1 parent 26116bc commit 0b8b330

File tree

5 files changed

+14
-28
lines changed

5 files changed

+14
-28
lines changed

README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Demo of Factoring
33

44
Integer factoring is the decomposition of an integer into factors that, when multiplied together, give the original
55
number. For example, the factors of 15 are 3 and 5.
6-
6+
77
D-Wave quantum computers allow us to factor numbers in an entirely new way, by turning a multiplication circuit into a
88
constraint satisfaction problem that allows the quantum computer to compute inputs from a predefined output.
99
Essentially, this means running the multiplication circuit in reverse!
@@ -38,13 +38,13 @@ appropriate location for the given OS and virtual environment configuration. Thi
3838
>>> from penaltymodel_cache import cache_file
3939
>>> cache_file()
4040
'/home/bellert/git_root/factoring-demo/env/data/dwave-penaltymodel-cache/penaltymodel_cache_v0.2.0.db'
41-
41+
4242
A minimal working example using the main interface function can be seen by running:
4343

4444
.. code-block:: bash
4545
4646
python demo.py
47-
47+
4848
The user is prompted to enter a six-bit integer: P, which represents a product to be factored.
4949

5050
.. code-block::
@@ -53,12 +53,12 @@ The user is prompted to enter a six-bit integer: P, which represents a product t
5353
5454
The algorithm returns possible A and B values, which are the inputs the circuit multiplies to calculate the product, P.
5555

56-
By default, the function that sends the problem to the QPU uses a saved embedding. This can be overriden and the code to
57-
calculate a new embedding can be called by using:
56+
By default, the main interface function uses a saved embedding. This can be overriden and the code to calculate a new
57+
embedding can be called by using:
5858

5959
.. code-block:: python
6060
61-
submit_factor_bqm(bqm, use_saved_embedding=False)
61+
factor(P, use_saved_embedding=False)
6262
6363
License
6464
-------

demo.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pprint import pprint
44

5-
from factoring.interfaces import get_factor_bqm, submit_factor_bqm, postprocess_factor_response
5+
from factoring.interfaces import factor
66

77
_PY2 = sys.version_info.major == 2
88
if _PY2:
@@ -33,12 +33,10 @@ def sanitised_input(description, variable, range_):
3333
# get input from user
3434
print("Enter a number to be factored:")
3535
P = sanitised_input("product", "P", range(2 ** 6))
36-
bqm = get_factor_bqm(P)
3736

3837
# send problem to QPU
3938
print("Running on QPU")
40-
response = submit_factor_bqm(bqm)
39+
output = factor(P)
4140

4241
# output results
43-
output = postprocess_factor_response(response, P)
4442
pprint(output)

factoring/interfaces.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def validate_input(ui, range_):
2626
raise ValueError("Input must be between {} and {}".format(start, stop))
2727

2828

29-
def get_factor_bqm(P):
29+
def factor(P, use_saved_embedding=True):
3030
validate_input(P, range(2 ** 6))
3131

3232
# get circuit
@@ -43,10 +43,6 @@ def get_factor_bqm(P):
4343
bqm.fix_variable(var, value)
4444
log.debug('bqm construction time: %s', time.time() - construction_start_time)
4545

46-
return bqm
47-
48-
49-
def submit_factor_bqm(bqm, use_saved_embedding=True):
5046
# find embedding and put on system
5147
sampler = DWaveSampler()
5248

@@ -82,10 +78,6 @@ def submit_factor_bqm(bqm, use_saved_embedding=True):
8278
response = dimod.unembed_response(response, embedding, source_bqm=bqm)
8379
logging.debug('embedding and sampling time: %s', time.time() - sample_time)
8480

85-
return response
86-
87-
88-
def postprocess_factor_response(response, P):
8981
output = {"results": [],
9082
# {
9183
# "a": Number,

factoring/package_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.0.4'
1+
__version__ = '0.1.0'
22
__author__ = 'D-Wave Systems Inc.'
33
__authoremail__ = '[email protected]'
44
__description__ = 'Demo of factoring a 6-bit integer by embedding a 3-bit multiplier on the D-Wave QPU'

tests/test_interfaces.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import unittest
22
from random import randint
33

4-
from factoring.interfaces import get_factor_bqm, submit_factor_bqm, postprocess_factor_response
4+
from factoring.interfaces import factor
55

66

77
class TestInterfaces(unittest.TestCase):
88

99
def test_factor_output(self):
1010
P = randint(0, 2**6-1)
11-
bqm = get_factor_bqm(P)
12-
response = submit_factor_bqm(bqm)
13-
output = postprocess_factor_response(response, P)
11+
output = factor(P)
1412

1513
self.assertSetEqual(set(output), {'results', 'numberOfReads'})
1614
for result in output['results']:
@@ -24,13 +22,11 @@ def test_factor_output(self):
2422

2523
def test_factor_invalid(self):
2624
for P in [-1, 64, 'a']:
27-
self.assertRaises(ValueError, get_factor_bqm, P)
25+
self.assertRaises(ValueError, factor, P)
2826

2927
@unittest.skip("not robust enough yet")
3028
def test_factor_validity(self):
3129
for P in {a*b for a in range(2**3) for b in range(2**3)}:
32-
bqm = get_factor_bqm(P)
33-
response = submit_factor_bqm(bqm)
34-
output = postprocess_factor_response(response, P)
30+
output = factor(P)
3531

3632
self.assertTrue(output['results'][0]['valid'])

0 commit comments

Comments
 (0)