qiskit-metapackage icon indicating copy to clipboard operation
qiskit-metapackage copied to clipboard

Why is it impossible to simulate CDKMRippleCarryAdder

Open adaszews opened this issue 3 years ago • 3 comments

Informations

  • Qiskit version: 0.18.2
  • Python version: 3.9.5
  • Operating system: Linux

What is the current behavior?

Simulation failed and returned the following error message:
ERROR: Failed to load qobj: AerSimulator: circuit with instructions {"instructions": {gate, save_statevector}, "gates": {x, CDKMRippleCarryAdder}} is not compatible with any available simulation methods

Steps to reproduce the problem

p = QuantumCircuit(8)
p.x(0)
q = QuantumCircuit(8)
q.x(0)

adder = circuit.library.arithmetic.adders.cdkm_ripple_carry_adder.CDKMRippleCarryAdder(8)
adder.compose(p, [1,2,3,4,5,6,7,8], inplace=True, front=True)
adder.compose(q, [9,10,11,12,13,14,15,16], inplace=True, front=True)

backend = Aer.get_backend('statevector_simulator')
job = backend.run(adder)

What is the expected behavior?

Simulation should run

Suggested solutions

Fix the simulator or CDKMRippleCarryAdder implementaion?

adaszews avatar Sep 23 '21 07:09 adaszews

When running a circuit through backend.run, the backend expects the circuit to already be in a format it can directly execute (contain only gates in its basis_gates, or 2q gates in its coupling_map, ...). This can be handled automatically with the transpiler:

backend = Aer.get_backend('statevector_simulator')
transpiled_adder = transpile(adder, backend)
job = backend.run(transpiled_adder)

kdk avatar Sep 27 '21 16:09 kdk

I encountered the same problem when using qiskit. When I use the measurement operation, it will report an error. When I don't use the measurement operation. The program will not report an error. I later used the transpile function, which is useless.

Here is my code:

from qiskit import *
from qiskit.circuit.library import XGate
qr = QuantumRegister(36, "qr")
cr = ClassicalRegister(36, 'cr')
qc = QuantumCircuit(qr, cr)
for i in range(8, 12):
    qc.h(i)
gate = XGate().control(num_ctrl_qubits=4, ctrl_state='1111')
qc.append(gate, [8,9,10,11]+[4])
qc.barrier()
for i in range(36):
    qc.measure(i, i)
qc.draw(output='mpl', filename='out')
backend_sim = Aer.get_backend('qasm_simulator')
job_sim = execute(qc, backend_sim, shots=1024)
result_sim = job_sim.result()
counts = result_sim.get_counts(qc)
res = []
for i in counts.keys():
    res.append(i)
print(res)

Arvin996 avatar Oct 08 '21 02:10 Arvin996

Hi @Arvin996 , thanks for reporting but this looks like a separate issue. In your case, it looks like the simulation method you're using likely exceeds your available memory for a 36 qubit circuit. e.g. when running your code, I get the following error message: image (this is also available in job_sim.result().status.

You can update your code to use e.g. the matrix product simulator as below:

from qiskit.providers.aer import AerSimulator
backend_sim = AerSimulator(method='matrix_product_state')

kdk avatar Oct 08 '21 22:10 kdk

I'm going to close this as stale now, because I think the original question was answered. Please feel free to open a new issue on Terra if there's more to ask.

jakelishman avatar Mar 20 '23 13:03 jakelishman