# What is the right way to prepare a specific qubit state in Qiskit / Q#?

There are two possibilities to prepare a qubit in a particular state like $$| - \rangle$$ in Qiskit.

The first approach is using the initialize function:

qc = QuantumCircuit(1)
initial_state = [1/sqrt(2),-1/sqrt(2)]
qc.initialize(initial_state, 0)


The second approach is using standard quantum gates:

qreg_q = QuantumRegister(1, 'q')
creg_c = ClassicalRegister(1, 'c')
qc = QuantumCircuit(qreg_q, creg_c)
qc.x(qreg_q[0])
qc.h(qreg_q[0])


Are these two possibilities different? What is the "best practice" for preparing an arbitrary qubit state?

Is there a comparable initialize function in Q#?

(Disclaimer: I don't know what's going on "under the hood" in Qiskit's initialize, my answer is based on Q# part of the question, but I suspect it's something similar.)

The state of a quantum system is not something that can be directly initialized to the necessary state (quantum computing would've been a lot easier if it was possible!) Instead we prepare the states by starting in a well-defined state (typically a state of all $$|0\rangle$$s) and applying the necessary gates to transform the system to the necessary state.

In Q#, the corresponding library operation is PrepareArbitraryState which takes an array of coefficients and prepares a quantum state with these coefficients. You can see that "under the hood" it compiles a sequence of gates necessary to prepare the state depending on the given coefficients and applies them to the qubits.

To return to your original question, the options you're considering might end up being identical (if you have a simple state, initialize will probably compile it to the same gates as the ones you've written out manually). The factors to consider are:

• If you're looking to write code that is easy to change if you realize you need a different state, you'll probably want to use the library operation.
• Same goes if you're looking to initialize a system to a fairly complex state and you don't want to figure out all the rotations by hand.
• On the other hand, if you're looking to learn more the logic behind state preparation and to practice applying gates to change the system state, you'll want to do it by hand (like Superposition quantum kata offers you to).
• There is a difference if what's executed is a simulator instead of a real device. In this case, depending on the simulator, initialize may set the initial state directly. Aug 10 '20 at 20:09
• Was it proven that any state with any number of qubits can be reached ? Aug 11 '20 at 9:09
• The state prep used by PrepareArbitraryState follows this paper arxiv.org/abs/quant-ph/0406176, so I think so. Aug 11 '20 at 16:51