In PyQuil, a qubit is initialised as $|0\rangle$ by default. Suppose, I want to initialise a qubit in an arbitrary state, say $\frac{1}{\sqrt{3}}|0\rangle+\sqrt{\frac{2}{3}}|1\rangle$. How to do this arbitrary qubit initialisation in PyQuil?


1 Answer 1


As far as I know, you can only do this with the ReferenceDensitySimulator, which contains a set_initial_state method. This is unfortunate because density matrix simulation is very costly.

In your case:

from pyquil import Program
from pyquil.simulation import ReferenceDensitySimulator
from pyquil.pyqvm import PyQVM
from pyquil.gates import I

state_ket = np.array([1/np.sqrt(3), np.sqrt(2/3)])
state_bra =  np.array([[1/np.sqrt(3)], [np.sqrt(2/3)]])

rho1 = state_ket * state_bra

prog = Program(I(0))
qam = PyQVM(n_qubits=1, quantum_simulator_type=ReferenceDensitySimulator)

# must call reset() to change the current state of the density matrix
density_matrix = qam.wf_simulator.set_initial_state(rho1).reset()
np.testing.assert_array_equal(qam.wf_simulator.density, rho1)

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.