Quantum Operations

CUDA-Q provides a default set of quantum operations on qubits. These operations can be used to define custom kernels and libraries. Since the set of quantum intrinsic operations natively supported on a specific target depends on the backends architecture, the nvq++ compiler automatically decomposes the default operations into the appropriate set of intrinsic operations for that target.

The sections Unitary Operations on Qubits and Measurements on Qubits list the default set of quantum operations on qubits.

Operations that implement unitary transformations of the quantum state are templated. The template argument allows to invoke the adjoint and controlled version of the quantum transformation, see the section on Adjoint and Controlled Operations.

CUDA-Q additionally provides overloads to support broadcasting of single-qubit operations across a vector of qubits. For example, x(cudaq::qvector<>&) flips the state of each qubit in the provided cudaq::qvector.

Unitary Operations on Qubits

x

This operation implements the transformation defined by the Pauli-X matrix. It is also known as the quantum version of a NOT-gate.

qubit = cudaq.qubit()

# Apply the unitary transformation
# X = | 0  1 |
#     | 1  0 |
x(qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// X = | 0  1 |
//     | 1  0 |
x(qubit);

y

This operation implements the transformation defined by the Pauli-Y matrix.

qubit = cudaq.qubit()

# Apply the unitary transformation
# Y = | 0  -i |
#     | i   0 |
y(qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// Y = | 0  -i |
//     | i   0 |
y(qubit);

z

This operation implements the transformation defined by the Pauli-Z matrix.

qubit = cudaq.qubit()

# Apply the unitary transformation
# Z = | 1   0 |
#     | 0  -1 |
z(qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// Z = | 1   0 |
//     | 0  -1 |
z(qubit);

h

This operation is a rotation by π about the X+Z axis, and enables one to create a superposition of computational basis states.

qubit = cudaq.qubit()

# Apply the unitary transformation
# H = (1 / sqrt(2)) * | 1   1 |
#                     | 1  -1 |
h(qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// H = (1 / sqrt(2)) * | 1   1 |
//                     | 1  -1 |
h(qubit);

r1

This operation is an arbitrary rotation about the |1> state.

qubit = cudaq.qubit()

# Apply the unitary transformation
# R1(λ) = | 1     0    |
#         | 0  exp(iλ) |
r1(math.pi, qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// R1(λ) = | 1     0    |
//         | 0  exp(iλ) |
r1(std::numbers::pi, qubit);

rx

This operation is an arbitrary rotation about the X axis.

qubit = cudaq.qubit()

# Apply the unitary transformation
# Rx(θ) = |  cos(θ/2)  -isin(θ/2) |
#         | -isin(θ/2)  cos(θ/2)  |
rx(math.pi, qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// Rx(θ) = |  cos(θ/2)  -isin(θ/2) |
//         | -isin(θ/2)  cos(θ/2)  |
rx(std::numbers::pi, qubit);

ry

This operation is an arbitrary rotation about the Y axis.

qubit = cudaq.qubit()

# Apply the unitary transformation
# Ry(θ) = | cos(θ/2)  -sin(θ/2) |
#         | sin(θ/2)   cos(θ/2) |
ry(math.pi, qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// Ry(θ) = | cos(θ/2)  -sin(θ/2) |
//         | sin(θ/2)   cos(θ/2) |
ry(std::numbers::pi, qubit);

rz

This operation is an arbitrary rotation about the Z axis.

qubit = cudaq.qubit()

# Apply the unitary transformation
# Rz(λ) = | exp(-iλ/2)      0     |
#         |     0       exp(iλ/2) |
rz(math.pi, qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// Rz(λ) = | exp(-iλ/2)      0     |
//         |     0       exp(iλ/2) |
rz(std::numbers::pi, qubit);

s

This operation applies to its target a rotation by π/2 about the Z axis.

qubit = cudaq.qubit()

# Apply the unitary transformation
# S = | 1   0 |
#     | 0   i |
s(qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// S = | 1   0 |
//     | 0   i |
s(qubit);

t

This operation applies to its target a π/4 rotation about the Z axis.

qubit = cudaq.qubit()

# Apply the unitary transformation
# T = | 1      0     |
#     | 0  exp(iπ/4) |
t(qubit)
cudaq::qubit qubit;

// Apply the unitary transformation
// T = | 1      0     |
//     | 0  exp(iπ/4) |
t(qubit);

swap

This operation swaps the states of two qubits.

qubit_1, qubit_2 = cudaq.qubit(), cudaq.qubit()

# Apply the unitary transformation
# Swap = | 1 0 0 0 |
#        | 0 0 1 0 |
#        | 0 1 0 0 |
#        | 0 0 0 1 |
swap(qubit_1, qubit_2)
cudaq::qubit qubit_1, qubit_2;

// Apply the unitary transformation
// Swap = | 1 0 0 0 |
//        | 0 0 1 0 |
//        | 0 1 0 0 |
//        | 0 0 0 1 |
swap(qubit_1, qubit_2);

Adjoint and Controlled Operations

The adj method of any gate can be used to invoke the adjoint transformation:

# Create a kernel and allocate a qubit in a |0> state.
qubit = cudaq.qubit()

# Apply the unitary transformation defined by the matrix
# T = | 1      0     |
#     | 0  exp(iπ/4) |
# to the state of the qubit `q`:
t(qubit)

# Apply its adjoint transformation defined by the matrix
# T† = | 1      0     |
#      | 0  exp(-iπ/4) |
t.adj(qubit)
# `qubit` is now again in the initial state |0>.

The ctrl method of any gate can be used to apply the transformation conditional on the state of one or more control qubits, see also this Wikipedia entry.

# Create a kernel and allocate qubits in a |0> state.
ctrl_1, ctrl_2, target = cudaq.qubit(), cudaq.qubit(), cudaq.qubit()
# Create a superposition.
h(ctrl_1)
# `ctrl_1` is now in a state (|0> + |1>) / √2.

# Apply the unitary transformation
# | 1  0  0  0 |
# | 0  1  0  0 |
# | 0  0  0  1 |
# | 0  0  1  0 |
x.ctrl(ctrl_1, ctrl_2)
# `ctrl_1` and `ctrl_2` are in a state (|00> + |11>) / √2.

# Set the state of `target` to |1>:
x(target)
# Apply the transformation T only if both
# control qubits are in a |1> state:
t.ctrl([ctrl_1, ctrl_2], target)
# The qubits ctrl_1, ctrl_2, and target are now in a state
# (|000> + exp(iπ/4)|111>) / √2.

The template argument cudaq::adj can be used to invoke the adjoint transformation:

// Allocate a qubit in a |0> state.
cudaq::qubit qubit;

// Apply the unitary transformation defined by the matrix
// T = | 1      0     |
//     | 0  exp(iπ/4) |
// to the state of the qubit `q`:
t(qubit);

// Apply its adjoint transformation defined by the matrix
// T† = | 1      0     |
//      | 0  exp(-iπ/4) |
t<cudaq::adj>(qubit);
// Qubit `q` is now again in the initial state |0>.

The template argument cudaq::ctrl can be used to apply the transformation conditional on the state of one or more control qubits, see also this Wikipedia entry.

// Allocate qubits in a |0> state.
cudaq::qubit ctrl_1, ctrl_2, target;
// Create a superposition.
h(ctrl_1);
// Qubit ctrl_1 is now in a state (|0> + |1>) / √2.

// Apply the unitary transformation
// | 1  0  0  0 |
// | 0  1  0  0 |
// | 0  0  0  1 |
// | 0  0  1  0 |
x<cudaq::ctrl>(ctrl_1, ctrl_2);
// The qubits ctrl_1 and ctrl_2 are in a state (|00> + |11>) / √2.

// Set the state of `target` to |1>:
x(target);
// Apply the transformation T only if both
// control qubits are in a |1> state:
t<cudaq::ctrl>(ctrl_1, ctrl_2, target);
// The qubits ctrl_1, ctrl_2, and target are now in a state
// (|000> + exp(iπ/4)|111>) / √2.

Following common convention, by default the transformation is applied to the target qubit(s) if all control qubits are in a |1> state. However, that behavior can be changed to instead apply the transformation when a control qubit is in a |0> state by negating the polarity of the control qubit. The syntax for negating the polarity is the not-operator preceding the control qubit:

cudaq::qubit c, q;
h(c);
x<cudaq::ctrl>(!c, q);
// The qubits c and q are in a state (|01> + |10>) / √2.

This notation is only supported in the context of applying a controlled operation and is only valid for control qubits. For example, negating either of the target qubits in the swap operation is not allowed. Negating the polarity of control qubits is similarly supported when using cudaq::control to conditionally apply a custom quantum kernel.

Measurements on Qubits

mz

This operation measures a qubit with respect to the computational basis, i.e., it projects the state of that qubit onto the eigenvectors of the Pauli-Z matrix. This is a non-linear transformation, and no template overloads are available.

qubit = cudaq.qubit()
mz(qubit)
cudaq::qubit qubit;
mz(qubit);

mx

This operation measures a qubit with respect to the Pauli-X basis, i.e., it projects the state of that qubit onto the eigenvectors of the Pauli-X matrix. This is a non-linear transformation, and no template overloads are available.

qubit = cudaq.qubit()
mx(qubit)
cudaq::qubit qubit;
mx(qubit);

my

This operation measures a qubit with respect to the Pauli-Y basis, i.e., it projects the state of that qubit onto the eigenvectors of the Pauli-Y matrix. This is a non-linear transformation, and no template overloads are available.

qubit = cudaq.qubit()
kernel.my(qubit)
cudaq::qubit qubit;
my(qubit);