In PennyLane, unitary operations specified by a matrix can be implemented in a quantum circuit using the QubitUnitary operation. QubitUnitary is a parametrized gate, and can be called like so:

qml.QubitUnitary(U, wires=wire)

Complete the quantum function below to create a circuit that applies U to the qubit and returns its state. (Compare this to the earlier function apply_u that you wrote before - isn't it nice to not have to worry about the matrix arithmetic?)

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

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


@qml.qnode(dev)
def apply_u():

##################
# YOUR CODE HERE #
##################

# USE QubitUnitary TO APPLY U TO THE QUBIT

# Return the state
return qml.state()

or to submit your code

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

Learning Objectives:

  • Define what it means for a matrix to be unitary.
  • Express a single-qubit unitary operation in terms of 3 real parameters.