3
$\begingroup$

I am writing a small algorithm to multiply two numbers using IBM's QISkit. The code is below:

times_shell.py

import times

first = input("Enter a number with less than 7 digits.")
l1 = len(first)
second = input("Enter another number with less than " + str(8-l1) + " 
digits.")
l2 = len(second)
if l1 > l2:
    n = l1
    m = l2
else:
    first, second = second, first
    n = l2
    m = l1

prod = ("0")*(m+n)

while int(second) is not 0:
    second, prod = times.multiply(first, second, prod, n, m)

times.py

def multiply(first, second, product, n, m):

    a = QuantumRegister(m+n, "a") #accumulator
    b = QuantumRegister(m+n, "b") #holds multiplicand
    c = QuantumRegister(m, "c") #hold multiplier
    d = QuantumRegister(m, "d") #register with value 1
    cl = ClassicalRegister(m+n, "cl") #used for final output
    cl2 = ClassicalRegister(m, "cl2")
    qc = QuantumCircuit(a, b, c, d, cl, cl2, name="qc")

    for i in range(0, m+n):
        if product[i] == "1":  
            qc.x(a[m+n-(i+1)])

    for i in range(0, n):
        if first[i] == "1":
            qc.x(b[n-(i+1)])

    for i in range(0, m):
        if second[i] == "1":
            qc.x(c[m-(i+1)])

    qc.x(d[0])

    for i in range(0, m+n):
        createInputState(qc, a, m+n-(i+1), pie)

    for i in range(m):
        createInputState(qc, c, m-(i+1), pie)

    for i in range(0, m+n):
        evolveQFTState(qc, a, b, m+n-(i+1), pie) 

    for i in range(0, m):
        decrement(qc, c, d, m-(i+1), pie)

    for i in range(0, m):
        inverseQFT(qc, c, i, pie)

    for i in range(0, m+n):
        inverseQFT(qc, a, i, pie)

    for i in range(0, m+n):
        qc.measure(a[i], cl[i])

    for i in range(0, m):
        qc.measure(c[i], cl2[i])

    print(qc.qasm())

    register(Qconfig['APItoken'], Qconfig['url'])
    result = execute(qc, backend='ibmq_qasm_simulator', 
                  shots=1024).result()
    counts = result.get_counts("qc")
    print(counts)
    output = max(counts.items(), key=operator.itemgetter(1))[0]
    multiplier, accumulator = str(output).split(" ")

    print(multiplier)
    print(accumulator)

    return multiplier, accumulator

When I run it I get an error. The terminal output (the program output and the error) is as follows:

Traceback (most recent call last):
    File "times_shell.py", line 18, in <module> second, prod = 
    times.multiply(first, second, prod, n, m)
    File "D:\Projects\Quantum_Computing\IBM_Python\times.py", line 122, in 
    multiply
    register(Qconfig['APItoken'], Qconfig['url'])
    File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
    packages\qiskit\wrapper\_wrapper.py", line 56, in register
    _DEFAULT_PROVIDER.add_provider(provider)
    File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python37-32\lib\site- 
    packages\qiskit\wrapper\defaultqiskitprovider.py", line 158, in 
    add_provider
    raise QISKitError("The same provider has already been registered!")
    qiskit._qiskiterror.QISKitError: 'The same provider has already been 
    registered!'

I'm not sure what the issue is here. Any help with this issue would be appreciated.

$\endgroup$
  • $\begingroup$ Can you check the available backends from your credentials just in case? Qiskit provides such function to check. $\endgroup$ – cnada Sep 24 '18 at 18:57
  • $\begingroup$ Hi @cnada, I found a solution. I have elaborated upon the same in my answer below. $\endgroup$ – Sashwat Anagolum Sep 25 '18 at 15:20
2
$\begingroup$

It seems that the solution is rather simple - simply move the registration bit of the code, i.e. the bit below, to the times_shell.py script.

register(Qconfig['APItoken'], Qconfig['url'])

This way I only register once, and can run my job multiple times. The successful results of my algorithm can be seen below:

Input:

Enter a number with less than 7 digits.1111
Enter another number with less than 4 digits.11

Output:

{'10 101111': 459, '10 001111': 565}
10
001111

{'01 000010': 15, '01 101110': 130, '01 011110': 197, '01 010110': 38, '01 110110': 
78, '01 000110': 58, '01 110010': 14, '01 100010': 50, '01 010010': 5, '01 100110': 
71, '01 111010': 21, '01 101010': 30, '01 001010': 19, '01 011010': 14, '01 111110': 
169, '01 001110': 115}
01

{'00 101101': 664, '00 110001': 1, '00 101001': 128, '00 001001': 7, '00 111001': 18, 
'00 011101': 8, '00 001101': 163, '00 100001': 4, '00 111101': 31}
00
101101

I hope this helps anyone facing similar issues.

$\endgroup$
  • $\begingroup$ Thanks for documenting all the issues you've run into, I'm certain this will help others as they learn! $\endgroup$ – ahelwer Sep 25 '18 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.