Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

How to obtain wavefunction from quibit registers #369

Closed
yangjy0826 opened this issue Nov 8, 2018 · 4 comments
Closed

How to obtain wavefunction from quibit registers #369

yangjy0826 opened this issue Nov 8, 2018 · 4 comments
Assignees

Comments

@yangjy0826
Copy link

yangjy0826 commented Nov 8, 2018

Description

Hi, I found in this link that there is a way to obtain wavefunction from quibit registers, and an example is given:

import qiskit
from qiskit import QuantumProgram
import qiskit.extensions.qiskit_simulator

qp = QuantumProgram()
qr = qp.create_quantum_register('qr', 2);
cr = qp.create_classical_register('cr', 2);
circ = qp.create_circuit('bell', [qr], [cr])
circ.h(qr[0])
circ.cx(qr[0], qr[1])
circ.barrier(qr)
circ.save(1, qr)
circ.barrier(qr)
circ.measure(qr, cr)

backend = 'local_qiskit_simulator'
config = {'data': ['saved_quantum_state',
                   'saved_density',
                   'saved_quantum_state_ket',
                   'saved_probabilities']}
result = qp.execute('bell', shots=10, backend=backend, config=config)
result.get_data('bell')

its output is:

{'counts': {'00': 5, '11': 5},
 'saved_probabilities': {'1': [0.5, 0.0, 0.0, 0.5]},
 'saved_quantum_states': [{1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])},
  {1: array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])}],
 'saved_quantum_states_ket': [{'1': {'00': [0.707106781186548, 0.0],
    '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}},
  {'1': {'00': [0.707106781186548, 0.0], '11': [0.707106781186547, 0.0]}}],
 'time_taken': 0.000511}

But when I was trying the code, I find that QuantumProgram and qiskit.extensions.qiskit_simulator can't be imported successfully. I am not sure whether it's because of the updating of Qiskit version. Then how can I obtain the wavefunction now?

Thank you!

Your Environment

  • QISKit version: 0.6.0
  • Python version: 3.6
@yangjy0826
Copy link
Author

I searched for the Changelog, and found that in the Deprecated part of version 0.5.0, there is a sentence saying ''Simulators no longer return wavefunction by setting shots=1. Instead, use the local_statevector_simulator, or explicitly ask for snapshot.'' What does it mean?

@chriseclectic
Copy link
Member

That standard way to the get the final state vector is to use the statevector_simulator backend to execute a single shot of a circuit with no reset or measure instructions. For example:

import qiskit
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import Aer

qr = qiskit.QuantumRegister(2)
circ = qiskit.QuantumCircuit(qr)
circ.h(qr[0])
circ.cx(qr[0], qr[1])

backend = Aer.get_backend('statevector_simulator')
result = qiskit.execute(circ, backend=backend).result()
result.get_data(circ)

This will return the output

{'statevector': array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])}

You may also use result.get_statevector(circ) to directly get the np array from the output of the statevector simulator.

@yangjy0826
Copy link
Author

Thank you so much! This is exactly what I'm looking for.

@chriseclectic
Copy link
Member

No problem!

@attp attp closed this as completed Nov 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants