Measurements

[1]:
import cudaq

Kernel measurement can be specified in the Z, X, or Y basis using mz, mx, and my. If a measurement is specified with no argument, the entire kernel is measured in that basis. Measurement occurs in the Z basis by default.

[2]:
@cudaq.kernel
def kernel():
    qubits = cudaq.qvector(2)
    mz()

Specific qubits or registers can be measured rather than the entire kernel.

[3]:
@cudaq.kernel
def kernel():
    qubits_a = cudaq.qvector(2)
    qubit_b = cudaq.qubit()
    mz(qubits_a)
    mx(qubit_b)

Midcircuit Measurement and Conditional Logic

In certain cases, it it is helpful for some operations in a quantum kernel to depend on measurement results following previous operations. This is accomplished in the following example by performing a Hadamard on qubit 0, then measuring qubit 0 and savig the result as b0. Then, an if statement performs a Hadamard on qubit 1 only if b0 is 1. Measuring this qubit 1 verifies this process as a 1 is the result 25% of the time.

[4]:
@cudaq.kernel
def kernel():
    q = cudaq.qvector(2)
    h(q[0])
    b0 = mz(q[0])
    if b0:
        h(q[1])
        mz(q[1])