Running the program in Quantum Lab, it shows error. I am looking for solution.

Here is a code I run:

circ = QuantumCircuit(3)


circ.cx(0, 1)

circ.cx(0, 2)

from qiskit import Aer

backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
result = job.result()   
outputstate =result.get_statevector(circ, decimals=3)

An here is an error I got:

NameError                                 Traceback (most recent call last)
<ipython-input-3-17b8e431afa9> in <module>

----> 1 circ = QuantumCircuit(3)

      2 # Add a H gate on qubit 0, putting this qubit in superposition.

      3 circ.h(0)

      4 # Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting

      5 # the qubits in a Bell state.

NameError: name 'QuantumCircuit' is not defined

You need to import QuantumCircuit from qiskit:

from qiskit import QuantumCircuit

I imagine you will probably also need to do:

from qiskit import execute

And you can combine your three imports neatly into one line like so:

from qiskit import Aer, QuantumCircuit, execute
| improve this answer | |
  • $\begingroup$ thanx u solved my problem. got the results $\endgroup$ – parth Sep 8 at 11: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.