# How can I represent mixed states in Q#?

I have a mixed state with a density matrices corresponding to $$\rho=\sum_0^{2^n-1}p_i|i\rangle\langle i|$$. How would i represent this in Q#? How would I go about applying Unitary Operations $$U$$ on this mixed state in Q#?

It can be helpful to step back and look at what a density matrix describes: a probability distribution over projectors onto pure states. In your example, for instance, $$\rho$$ represents a distribution in which $$|\psi\rangle = |i\rangle$$ with probability $$p_i$$. That is, a density operator describes a probability over an ensemble of state vectors.
From that perspective, we easily write pseudocode for a quantum program that prepares $$\rho = \sum_i p_i |\psi_i\rangle\langle\psi_i|$$:
• Draw a random $$i$$ with probability $$p_i$$.
• Prepare the pure state $$|\psi_i\rangle$$.
Thinking of state vectors like $$|\psi_i\rangle$$ as quantum programs that prepare those states, this is already a complete description of how to prepare $$\rho$$. You can, for instance, use the PrepareArbitraryState operation to prepare each $$|\psi_i\rangle$$ on a freshly allocated register of qubits.
In practice, however, you'll often have a much more efficient implementation of the state preparation programs for each $$|\psi_i\rangle$$. For example, there's no need to use PrepareArbitraryState operation to prepare $$|+\rangle$$, as the H operation does just fine.
This is, for instance, how the PrepareSingleQubitIdentity operation prepares the particular mixed state $$\rho = 𝟙 / 2$$ on a single qubit.