TransWikia.com

How to initialize a random state in Qiskit?

Quantum Computing Asked on February 24, 2021

I know I can make a random_circuit, but I see that Qiskit has random_statevector. I’d like to optimize computation time by initializing a state by that instead of putting a random circuit at the beginning. Is it possible and how do I do it? Thank you!

2 Answers

here is an example of how to initialize the random statevector then send it to the quantum circuit:

from qiskit.quantum_info import random_statevector, Statevector
from qiskit import QuantumCircuit
# import numpy as np

vec = random_statevector(2)
# vec = Statevector([1, 0])
print('before circuit: {}'.format(vec)) 

qc = QuantumCircuit(1)
qc.x(0)
qc.draw()

new_vec = vec.evolve(qc)
print('after circuit: {}'.format(new_vec))

you should notice that the dim of state vector is 2 so the quantum circuit is 1 qubit.

Correct answer by immaryw on February 24, 2021

How about using the data attribute, like this:

qc = QuantumCircuit(1)   
initial_state = qc.initialize(random_statevector(2).data, 0)

Answered by dda on February 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP