1
$\begingroup$

I am looking at the homepage of Qrisp, and they have an example of how to multiply two registers with Qiskit:

from qiskit import (QuantumCircuit, QuantumRegister,
ClassicalRegister, Aer, execute)
from qiskit.circuit.library import RGQFTMultiplier
n = 6
a = QuantumRegister(n)
b = QuantumRegister(n)
res = QuantumRegister(2*n)
cl_res = ClassicalRegister(2*n)
qc = QuantumCircuit(a, b, res, cl_res)
for i in range(len(a)):
    if 3 & 1<<i: qc.x(a[i])
for i in range(len(b)):
    if 4 & 1<<i: qc.x(b[i])
qc.append(RGQFTMultiplier(n, 2*n),
list(a) + list(b) + list(res))
qc.measure(res, cl_res)
backend = Aer.get_backend('qasm_simulator')
counts_dic = execute(qc, backend).result().get_counts()
print({int(k, 2) : v for k, v in counts_dic.items()})
#Yields: {12: 1024}

However, this no longer works in Qiskit 1.0.

I tried moving the Aer backend to from qiskit_aer import AerSimulator as mentioned here: Docs, and got this code:

from qiskit import (QuantumCircuit, QuantumRegister,
ClassicalRegister, transpile)
from qiskit.circuit.library import RGQFTMultiplier
from qiskit_aer import AerSimulator

n = 6
a = QuantumRegister(n)
b = QuantumRegister(n)
res = QuantumRegister(2*n)
cl_res = ClassicalRegister(2*n)
qc = QuantumCircuit(a, b, res, cl_res)
for i in range(len(a)):
    if 3 & 1<<i: qc.x(a[i])
for i in range(len(b)):
    if 4 & 1<<i: qc.x(b[i])
qc.append(RGQFTMultiplier(n, 2*n),
list(a) + list(b) + list(res))
qc.measure(res, cl_res)
backend = AerSimulator()
# backend = AerSimulator.get_backend('qasm_simulator')
# new_circuit = transpile(qc, backend)
job = backend.run([qc], shots=1024)
print(job.result())
counts_dic = job.result().get_counts()
print({int(k, 2) : v for k, v in counts_dic.items()})
#Yields: {12: 1024}

But now I get this error:

AerError: 'unknown instruction: RGQFTMultiplier'

So I am looking for an actual working implementation of a Qiskit multiplier to multiply 3 and 4.

$\endgroup$

2 Answers 2

6
$\begingroup$

I think that the problem is that you are passing a non-transpiled circuit to the job execution. I do not know why transpilation is not part of the run command.

If you uncomment your line:

# new_circuit = transpile(qc, backend)

and then modify the following line as

job = backend.run([new_circuit], shots=1024)

Then it should work.

$\endgroup$
1
  • $\begingroup$ Thanks! I commented it out due to a previous error and didn't catch it when it was fixed. $\endgroup$
    – Amir Naveh
    Commented Jun 10 at 8:16
1
$\begingroup$

Similarly to other backends, Aer only supports a limited amount of basis gates. Thus, you have to first transpile your circuit to work with your desired backend. So, simply uncommenting the corresponding line in your code and using it to create your job variable returns the correct result:

from qiskit import (QuantumCircuit, QuantumRegister,
ClassicalRegister, transpile)
from qiskit.circuit.library import RGQFTMultiplier
from qiskit_aer import AerSimulator

n = 6
a = QuantumRegister(n)
b = QuantumRegister(n)
res = QuantumRegister(2*n)
cl_res = ClassicalRegister(2*n)
qc = QuantumCircuit(a, b, res, cl_res)
for i in range(len(a)):
    if 3 & 1<<i: qc.x(a[i])
for i in range(len(b)):
    if 4 & 1<<i: qc.x(b[i])
qc.append(RGQFTMultiplier(n, 2*n),
list(a) + list(b) + list(res))
qc.measure(res, cl_res)
backend = AerSimulator()
# backend = AerSimulator.get_backend('qasm_simulator')
new_circuit = transpile(qc, backend)
job = backend.run([new_circuit], shots=1024)
print(job.result())
counts_dic = job.result().get_counts()
print({int(k, 2) : v for k, v in counts_dic.items()})
#Yields: {12: 1024}
$\endgroup$
1
  • $\begingroup$ Thanks! I commented it out due to a previous error and didn't catch it when it was fixed. $\endgroup$
    – Amir Naveh
    Commented Jun 10 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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