2
$\begingroup$

I have the code below which initalized a state in a quantum circuit then tries to perform some simulation. This doesn't work if I keep the initialize line uncommented, but runs just fine if I comment it out. It seems to error out at the get_unitary line. How can I initialize a circuit and also perform a simulation on it?

Error message qiskit.exceptions.QiskitError: 'Data for experiment "circuit-X" could not be found.'

circuit = QuantumCircuit(2)
circuit.initialize('01', circuit.qubits)
circuit.h(0)
circuit.h(1)
circuit.cx(0, 1)
circuit.cx(1, 0)
circuit.save_unitary(label="unitary", pershot=False)
simulator = Aer.get_backend('aer_simulator')
result = simulator.run(circuit).result()
unitary = result.get_unitary(circuit)
print(unitary.round(3))
$\endgroup$

1 Answer 1

2
$\begingroup$

TL;DR There are two issues with your code, the first one is that initialize introduces a reset. Consider using prepare_state (see docs) instead. The second one, is that you need to transpile the circuit before running it in a backend.

Problem one: initialize
The initialize instruction introduced a reset in the front, as part of the state preparation.

from qiskit import QuantumCircuitrcuit
circuit = QuantumCircuit(2)
circuit.initialize('01', circuit.qubits)
circuit.decompose().draw('mpl')

two resets, followed by State Preparation

You can, instead, use prepare_state directly:

from qiskit import QuantumCircuitrcuit
circuit = QuantumCircuit(2)
circuit.prepare_state('01', circuit.qubits)
circuit.draw('mpl')

state preparation

Problem two: transpilation
Not all backends support the instructions included in Qiskit, so you need to convert the circuit into one that the backend understands. This process is sometimes called transpilation.

Qiskit includes a transpiler that, given a circuit and a backend, converts that circuit into a new one that can run on that backend: circuit_transpiled = transpile(circuit, backend=simulator)

Conclusion: your working example:

from qiskit import QuantumCircuit, transpile
from qiskit_aer import Aer

circuit = QuantumCircuit(2)
# circuit.initialize('01', circuit.qubits)
circuit.prepare_state('01', circuit.qubits)

circuit.h(0)
circuit.h(1)
circuit.cx(0, 1)
circuit.cx(1, 0)
circuit.save_unitary()
simulator = Aer.get_backend('aer_simulator')

circuit_transpiled = transpile(circuit, backend=simulator)
result = simulator.run(circuit_transpiled).result()

unitary = result.get_unitary(circuit_transpiled.name)
print(unitary.round(3))
[[ 0.5+0.j  0.5+0.j  0.5-0.j  0.5+0.j]
 [-0.5-0.j  0.5+0.j  0.5+0.j -0.5-0.j]
 [-0.5-0.j  0.5+0.j -0.5+0.j  0.5+0.j]
 [ 0.5+0.j  0.5+0.j -0.5+0.j -0.5+0.j]]
$\endgroup$
4
  • 1
    $\begingroup$ Adding prepare_state errors out in result with qiskit_aer.aererror.AerError: 'unknown instruction: state_preparation'. Nothing useful comes up when searching the error message. $\endgroup$
    – Hercislife
    Apr 22, 2023 at 20:32
  • $\begingroup$ @Hercislife related: quantumcomputing.stackexchange.com/questions/34396/… $\endgroup$ Dec 7, 2023 at 20:40
  • $\begingroup$ I extended the answer with details and how to change the example to make it runnable. $\endgroup$
    – luciano
    Dec 8, 2023 at 13:11
  • $\begingroup$ This works, thank you so much! $\endgroup$
    – Hercislife
    Dec 11, 2023 at 14:56

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.