When quantum states in a Hilbert space are prepared probabilistically, they are represented via a density matrix acting on

Here, the state is prepared with probability .

PennyLane can handle density matrices quite well on its own. But for the sake of learning, let us build some density matrices by hand. Using np.outer or otherwise, complete the function below that returns the density matrix when the qubit state_1 is prepared with probability p_1, and the qubit state_2 with probability p_2. You may assume that the qubits are normalized.

def build_density_matrix(state_1, state_2, p_1, p_2):
"""Build the density matrix for two randomly prepared states.
Args:
state_1 (array[complex]): A normalized quantum state vector
state_2 (array[complex]): A second normalized quantum state vector
p_1 (float): The probability of preparing state_1
p_2 (float): The probability of preparing state_2
Returns:
(np.array([array[complex]])): The density matrix for the preparation.
"""
projector_1 = # Compute the outer product of state_1 with itself
projector_2 = # Compute the outer product of state_2 with itself
density_matrix = # Build the density matrix
return density_matrix

print("state_1 = |+y>, state_2 = |+x>, p_1 = 0.5, p_2 = 0.5")
print("density_matrix:")
print(build_density_matrix([1,1j]/np.sqrt(2), [1,1]/np.sqrt(2), 0.5, 0.5))


or to submit your code

To interact with codercises, please switch to a larger screen size.

Learning Objectives:

  • Define the concept of the density operator and state its properties.
  • Distinguish between pure and mixed states.