Issue with Factoring with D-Wave Quantum Computers example

Hello 

I have tried to factor numbers other than the "21" given in the example given in the Jupyter Notebook and got wrong answers. I tried 22 and 24. 

Did anyone encounter something similar?

Thanks

0

Comments

4 comments
  • Nasser,

    The factoring example in the Notebook is set up as a CSP for a multiplication circuit for multiplying two 3-bit numbers. Since 22 and 24 have factors which need more than 3 bits (that is outside the range 0-7), it cannot return a correct solution.

    To try factoring other numbers, increase the number of bits in the multiplicands (a and b) and the product (p).

    I hope this helps answer your question.

    Ed

    0
    Comment actions Permalink
  • Thank you very much. But I tested it with 4bits and it always gave bad results.

    This is the whole code:

    P = 22  

    from dimod.generators import multiplication_circuit 

    bqm = multiplication_circuit(4)

    p_vars = ['p0', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7']

    fixed_variables = dict(zip(reversed(p_vars), "{:08b}".format(P)))

    fixed_variables = {var: int(x) for(var, x) in fixed_variables.items()}

    for var, value in fixed_variables.items():

        bqm.fix_variable(var, value)

    from dwave.system import DWaveSampler

    sampler = DWaveSampler() 

    from dwave.system import EmbeddingComposite

    embedding_sampler = EmbeddingComposite(sampler)

    sampleset = embedding_sampler.sample(bqm, num_reads=100, label='Notebook - Factoring')

    from helpers.convert import to_base_ten

    a, b = to_base_ten(sampleset.first.sample)

     

    print("Given integer P={}, found factors a={} and b={}".format(P, a, b))

    Thanks again.

    0
    Comment actions Permalink
  • Thanks for including your code. From that, I can see the problem is not in your code in the main notebook. The extra bits (a3, b3) for handling 4-bit multiplicands also need to be added in the to_base_ten function in convert.py. Your sampler was finding the correct solution, but it was being printed incorrectly.

    Also, contrary to what the notebook says, I found I needed to increase the num_reads from the value of 100 to 1000 to make it more likely I would get a correct solution in the sample.

    If this does not solve it for you, please let us know what results you are seeing.

    Ed

    0
    Comment actions Permalink
  • Thanks a lot. It finally worked. I defined and changed  the convert function and increased the number of reads function in the code:

    P = 22  

    from dimod.generators import multiplication_circuit 

    bqm = multiplication_circuit(4)

    p_vars = ['p0', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7']

    fixed_variables = dict(zip(reversed(p_vars), "{:08b}".format(P)))

    fixed_variables = {var: int(x) for(var, x) in fixed_variables.items()}

    for var, value in fixed_variables.items():

        bqm.fix_variable(var, value)

    from dwave.system import DWaveSampler

    sampler = DWaveSampler() 

    from dwave.system import EmbeddingComposite

    embedding_sampler = EmbeddingComposite(sampler)

    sampleset = embedding_sampler.sample(bqm, num_reads=1000, label='Notebook - Factoring')

    def to_base_ten(sample):
        a = b = 0
        
        # we know that multiplication_circuit() has created these variables
        a_vars = ['a0', 'a1', 'a2','a3']
        b_vars = ['b0', 'b1', 'b2','b3']
        
        for lbl in reversed(a_vars):
            a = (a << 1) | sample[lbl]
        for lbl in reversed(b_vars):
            b = (b << 1) | sample[lbl] 
            
        return a,b


    a, b = to_base_ten(sampleset.first.sample)

     

    print("Given integer P={}, found factors a={} and b={}".format(P, a, b))

    You were of great help.

    0
    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post