In PennyLane, CNOTs can be applied using qml.CNOT and the following syntax:

def circuit(): qml.CNOT(wires=[control, target])

where control and target are the wire labels (e.g., qml.CNOT(wires=[0, 1])).

Write a circuit that implements a gate between two qubits. Test it out on all four computational basis states. What are the resulting states? Express your answer in a dictionary that takes the form of a truth table, i.e., a table that details a set of output bits given the set of input bits:

As an explicit example, the truth table of is

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


@qml.qnode(dev)
def apply_cnot(basis_id):
"""Apply a CNOT to |basis_id>.

Args:
basis_id (int): An integer value identifying the basis state to construct.

Returns:
np.array[complex]: The resulting state after applying CNOT|basis_id>.
"""

# Prepare the basis state |basis_id>
bits = [int(x) for x in np.binary_repr(basis_id, width=num_wires)]
qml.BasisState(bits, wires=[0, 1])

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

# APPLY THE CNOT

return qml.state()


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

# REPLACE THE BIT STRINGS VALUES BELOW WITH THE CORRECT ONES
cnot_truth_table = {"00": "00", "01": "00", "10": "00", "11": "00"}


or to submit your code

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

Learning Objectives:

  • Define and apply entangling operations to multi-qubit systems.
  • Define the controlled-NOT (CNOT) gate, and write its matrix representation.
  • Define and apply general controlled operations.