I am new to Qiskit and I am trying to implement some very simple 1-qubit gates.

I want to initialize the state of the qubit (to 0 and then to 1) and then apply the gate and print the new state of the qubit.

def I(input):

  q = QuantumRegister(1) 
  c = ClassicalRegister(1)
  qc = QuantumCircuit(q, c)

  if input == '1': #if input = 1, initialize the qubit to 1

  qc.iden(q[0] )

  qc.measure( q[0], c[0])

  backend = Aer.get_backend('statevector_simulator')
  result = qiskit.execute(qc, backend=backend, shots=1).result()
  output = result.data(qc)

  return output

And I call the function like this:

print('\nResults for the Iden gate')
for input in ['0','1']:
print('    Input',input,'gives output',I(input))

Now the output of all this is:

Results for the Iden gate
Input 0 gives output {'statevector': [[1.0, 0.0], [0.0, 0.0]], 'counts': {'0x0': 1}}
Input 1 gives output {'statevector': [[0.0, 0.0], [1.0, 0.0]], 'counts': {'0x1': 1}}

I know that the data() function returns the Instruction object, a list of Qubits objects and a list of classical bits with the order they added. So here the second list is the list with the qubits.

But when the qubit is '0', shouldn't it return a list with all 0? I am not sure about the meaning of the output.


1 Answer 1


The return from result.data(qc) is information about the result of running the circuit qc. In your example, there are 2 pieces of information returned - the statevector and the counts.

The statevector is a way of describing the state of the qubit, and the result contains the statevector from the end of the circuit. Each of the arrays in the overall array corresponds to a coefficient in front of one of the basis states, the first element is the real part and the second is the imaginary. So, for your Input 0 this statevector is saying at the end of the circuit the qubit was in the state
$(1+0i)\vert 0 \rangle + (0+0i)\vert 1 \rangle$, which is the same as saying the qubit is in the $\vert 0 \rangle$ state.

The counts is saying how many times each output occurred in the results, so for Input 0 0x0 (aka 0) was the result once, which can be seen from the 1. This field is mainly used with the qasm_simulator.

  • $\begingroup$ Thank you so much! Now everything makes sense. I am trying to follow the same procedure with 2qubits gate (CNOT) like this: qc.cx(q[0], q[1]) but I always receive this result : {'counts': {'0x0': 1}, 'statevector': [[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]} How can I read this? $\endgroup$ Dec 21, 2019 at 15:01
  • $\begingroup$ It works in the same way as the one qubit case, only now your basis states are in terms of 2 qubits. This means the coefficients correspond to $\vert 00 \rangle,\vert 01 \rangle, \vert 10\rangle, \vert 11 \rangle$ respectively. The order of this is simply counting up in binary, $\endgroup$
    – met927
    Dec 21, 2019 at 15:38

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.