A common use of the gate is in initializing the state of a qubit at the beginning of an algorithm. Quite often, we would like our qubits to start in state (which is the default in PennyLane), however there are many cases where we instead would like to start from Complete the function below by using qml.PauliX to initialize the qubit's state to or based on an input flag. Then, use qml.QubitUnitary to apply the provided unitary U.

Hint.

The PauliX operation is a non-parametrized gate, meaning to call it in PennyLane, all we need to do is specify the wires:

qml.PauliX(wires=wire)
dev = qml.device("default.qubit", wires=1)

U = np.array([[1, 1], [1, -1]]) / np.sqrt(2)


@qml.qnode(dev)
def varied_initial_state(state):
"""Complete the function such that we can apply the operation U to
either |0> or |1> depending on the input argument flag.

Args:
state (int): Either 0 or 1. If 1, prepare the qubit in state |1>,
otherwise, leave it in state 0.

Returns:
np.array[complex]: The state of the qubit after the operations.
"""
##################
# YOUR CODE HERE #
##################

# KEEP THE QUBIT IN |0> OR CHANGE IT TO |1> DEPENDING ON THE state PARAMETER

# APPLY U TO THE STATE

return qml.state()

or to submit your code

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

Learning Objectives:

  • Explain why we can understand how an operation works by applying it to the basis states.
  • Describe the action of the X gate, its matrix representation, and eigenvalues.
  • Describe the action of the Hadamard gate, its matrix representation, and eigenvalues.