0
$\begingroup$

Just as the title, I want to get the state of my quantum circuit when running simulation locally with fake provider. But I cannot use save_statevector since Aer has been removed from qiskit. My code is just as follows, any answer can be helpful.

from qiskit import QuantumCircuit
from qiskit import transpile
from qiskit.visualization import plot_histogram
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
import matplotlib.pyplot as plt
import numpy as np
# Get a fake backend from the fake provider
backend = FakeManilaV2()

# Create a simple circuit
circuit = QuantumCircuit(3)
circuit.h(0)
circuit.cx(0, 1)
circuit.cx(0, 2)
circuit.measure_all()
circuit.draw('mpl')
plt.show()

# Transpile the ideal circuit to a circuit that can be directly executed by the backend
transpiled_circuit = transpile(circuit, backend)
transpiled_circuit.draw('mpl')
plt.show()
transpiled_circuit.save_statevector()
# Run the transpiled circuit using the simulated fake backend
job = backend.run(transpiled_circuit)
result = job.result()
outputstate = np.real(result.get_statevector(transpiled_circuit, decimals=100))
$\endgroup$

2 Answers 2

3
$\begingroup$

Aer is not gone, the namespace integration into qiskit where you could import it from qiskit.aer no longer exists. If you pip install qiskit-aer you can import the same function from qiskit_aer see the Qiskit 1.0 Release note for this

BTW Qiskit Runtime now has a local testing mode for running locally against fake backend is thats of interest. Examples there show qiskit aer being used and imported from qiskit_aer

$\endgroup$
1
  • $\begingroup$ Thanks a lot! It's very useful for a beginner like me! $\endgroup$
    – MrEightL
    Apr 7 at 19:20
0
$\begingroup$

You can use from qiskit.providers.fake_provider import GenericBackendV2, for example (I have slightly changed your code)

from qiskit import QuantumCircuit
from qiskit import transpile
from qiskit.visualization import plot_histogram
from qiskit.providers.fake_provider import GenericBackendV2
import matplotlib.pyplot as plt
import numpy as np
# Get a fake backend from the fake provider
n_qubits = 3
backend = GenericBackendV2(n_qubits)

# Create a simple circuit
circuit = QuantumCircuit(n_qubits)
circuit.h(0)
circuit.cx(0, 1)
circuit.cx(0, 2)
circuit.measure_all()

# Transpile the ideal circuit to a circuit that can be directly executed by the backend
transpiled_circuit = transpile(circuit, backend)
transpiled_circuit.draw()

here you would get a plot of your circuit

transpiled_circuit.save_statevector()
# Run the transpiled circuit using the simulated fake backend
job = backend.run(transpiled_circuit)
result = job.result()
outputstate = np.real(result.get_statevector(transpiled_circuit, decimals=100))
outputstate

output:

array([1., 0., 0., 0., 0., 0., 0., 0.])

Notice that you get a sample of the circuit because of the circuit.measure_all() command. If you remove it you should get something like the following

output:

array([-5.00446433e-01,  0.00000000e+00,  1.11050218e-16,  0.00000000e+00,
        0.00000000e+00,  0.00000000e+00,  0.00000000e+00, -4.99553168e-01])
$\endgroup$
1
  • $\begingroup$ Thanks for the suggestion! That's just what i want! $\endgroup$
    – MrEightL
    Apr 7 at 19:20

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.