Given a one-qubit gate matrix U, construct a controlled-U on all pointer
    qubits.

def controlled(num_ptr_bits, U):
    """
    Given a one-qubit gate matrix U, construct a controlled-U on all pointer
    qubits.
    """
    d = 2 ** (1 + num_ptr_bits)
    m = np.eye(d)
    m[d - 2:, d - 2:] = U
    return m