CUDA Quantum Python API¶
Program Construction¶
- cudaq.make_kernel(*args, **kwargs)¶
Overloaded function.
- cudaq.make_kernel() cudaq.Kernel
Create and return a
Kernel
that accepts no arguments.- Returns:
An empty kernel function to be used for quantum program construction. This kernel is non-parameterized and accepts no arguments.
- Return type:
# Example: # Non-parameterized kernel. kernel = cudaq.make_kernel()
- cudaq.make_kernel(\*args) tuple
Create a
Kernel
that takes the provided types as arguments. Returns a tuple containing the kernel and aQuakeValue
for each kernel argument.Note
The following types are supported as kernel arguments:
int
,float
,list
/List
,cudaq.qubit
, orcudaq.qreg
.- Parameters:
*arguments – A variable amount of types for the kernel function to accept as arguments.
- Returns:
A tuple containing an empty kernel function and a
QuakeValue
handle for each argument that was passed intomake_kernel()
.- Return type:
tuple[Kernel, QuakeValue, ...]
# Example: # Parameterized kernel that accepts an `int` # and `float` as arguments. kernel, int_value, float_value = cudaq.make_kernel(int, float)
- cudaq.from_state(*args, **kwargs)¶
Overloaded function.
- cudaq.from_state(kernel: cudaq.Kernel, qubits: cudaq.QuakeValue, state: numpy.ndarray[]) None
Decompose the input state vector to a set of controlled operations and rotations within the provided
kernel
body.# Example: import numpy as np # Define our kernel. kernel = cudaq.make_kernel() # Allocate some qubits. qubits = kernel.qalloc(3) # Define a simple state vector. state = np.array([0,1], dtype=np.complex128) # Now calling `from_state`, we will provide the `kernel` and the # qubit/s that we'd like to evolve to the given `state`. cudaq.from_state(kernel, qubits, state)
- cudaq.from_state(state: numpy.ndarray[]) cudaq.Kernel
Decompose the given state vector into a set of controlled operations and rotations and return a valid, callable, CUDA Quantum kernel.
# Example: import numpy as np # Define a simple state vector. state = np.array([0,1], dtype=np.complex128) # Create and return a kernel that produces the given `state`. kernel = cudaq.from_state(state)
- class cudaq.Kernel¶
The
Kernel
provides an API for dynamically constructing quantum circuits. TheKernel
programmatically represents the circuit as an MLIR function using the Quake dialect.Note
See
make_kernel()
for theKernel
constructor.- arguments¶
The arguments accepted by the
Kernel
function. Read-only.- Type:
List[
QuakeValue
]
- qalloc(*args, **kwargs)¶
Overloaded function.
- qalloc(self: cudaq.Kernel) cudaq.QuakeValue
Allocate a single qubit and return a handle to it as a
QuakeValue
.- Returns:
A handle to the allocated qubit in the MLIR.
- Return type:
# Example: kernel = cudaq.make_kernel() qubit = kernel.qalloc()
- qalloc(self: cudaq.Kernel, qubit_count: int) cudaq.QuakeValue
Allocate a register of qubits of size
qubit_count
and return a handle to them as aQuakeValue
.- Parameters:
qubit_count (
int
) – The number of qubits to allocate.- Returns:
A handle to the allocated qubits in the MLIR.
- Return type:
# Example: kernel = cudaq.make_kernel() qubits = kernel.qalloc(10)
- qalloc(self: cudaq.Kernel, qubit_count: cudaq.QuakeValue) cudaq.QuakeValue
Allocate a register of qubits of size
qubit_count
(wherequbit_count
is an existingQuakeValue
) and return a handle to them as a newQuakeValue
.- Parameters:
qubit_count (
QuakeValue
) – The parameterized number of qubits to allocate.- Returns:
A handle to the allocated qubits in the MLIR.
- Return type:
# Example: # Create a kernel that takes an int as its argument. kernel, qubit_count = cudaq.make_kernel(int) # Allocate the variable number of qubits. qubits = kernel.qalloc(qubit_count)
- __str__()¶
- __str__(self: cudaq.Kernel) str
Return the
Kernel
as a string in its MLIR representation using the Quake dialect.
- __call__()¶
- __call__(self: cudaq.Kernel, \*args) None
Just-In-Time (JIT) compile
self
(Kernel
), and call the kernel function at the provided concrete arguments.- Parameters:
*arguments (Optional[Any]) – The concrete values to evaluate the kernel function at. Leave empty if the
target
kernel doesn’t accept any arguments.
# Example: # Create a kernel that accepts an int and float as its # arguments. kernel, qubit_count, angle = cudaq.make_kernel(int, float) # Parameterize the number of qubits by `qubit_count`. qubits = kernel.qalloc(qubit_count) # Apply an `rx` rotation on the first qubit by `angle`. kernel.rx(angle, qubits[0]) # Call the `Kernel` on the given number of qubits (5) and at a concrete angle (pi). kernel(5, 3.14)
- x()¶
- x(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a x gate to the given target qubit or qubits.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply x to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a x gate to the qubit/s. kernel.x(qubits)
- cx(*args, **kwargs)¶
Overloaded function.
- cx(self: cudaq.Kernel, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-x operation to the given target qubit, with the provided control qubit/s.
- Parameters:
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-x between the two qubits. kernel.cx(control=control, target=target)
- cx(self: cudaq.Kernel, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-x operation to the given target qubits, with the provided list of control qubits.
- Parameters:
controls (list[QuakeValue]) – The list of qubits to use as controls for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() controls = kernel.qalloc(2) target = kernel.qalloc() # Apply a controlled-x to the target qubit, with the two control qubits. kernel.cx(controls=[controls[0], controls[1]], target=target)
- y()¶
- y(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a y gate to the given target qubit or qubits.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply y to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a y gate to the qubit/s. kernel.y(qubits)
- cy(*args, **kwargs)¶
Overloaded function.
- cy(self: cudaq.Kernel, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-y operation to the given target qubit, with the provided control qubit/s.
- Parameters:
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-y between the two qubits. kernel.cy(control=control, target=target)
- cy(self: cudaq.Kernel, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-y operation to the given target qubits, with the provided list of control qubits.
- Parameters:
controls (list[QuakeValue]) – The list of qubits to use as controls for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() controls = kernel.qalloc(2) target = kernel.qalloc() # Apply a controlled-y to the target qubit, with the two control qubits. kernel.cy(controls=[controls[0], controls[1]], target=target)
- z()¶
- z(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a z gate to the given target qubit or qubits.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply z to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a z gate to the qubit/s. kernel.z(qubits)
- cz(*args, **kwargs)¶
Overloaded function.
- cz(self: cudaq.Kernel, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-z operation to the given target qubit, with the provided control qubit/s.
- Parameters:
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-z between the two qubits. kernel.cz(control=control, target=target)
- cz(self: cudaq.Kernel, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-z operation to the given target qubits, with the provided list of control qubits.
- Parameters:
controls (list[QuakeValue]) – The list of qubits to use as controls for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() controls = kernel.qalloc(2) target = kernel.qalloc() # Apply a controlled-z to the target qubit, with the two control qubits. kernel.cz(controls=[controls[0], controls[1]], target=target)
- h()¶
- h(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a h gate to the given target qubit or qubits.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply h to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a h gate to the qubit/s. kernel.h(qubits)
- ch(*args, **kwargs)¶
Overloaded function.
- ch(self: cudaq.Kernel, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-h operation to the given target qubit, with the provided control qubit/s.
- Parameters:
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-h between the two qubits. kernel.ch(control=control, target=target)
- ch(self: cudaq.Kernel, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-h operation to the given target qubits, with the provided list of control qubits.
- Parameters:
controls (list[QuakeValue]) – The list of qubits to use as controls for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() controls = kernel.qalloc(2) target = kernel.qalloc() # Apply a controlled-h to the target qubit, with the two control qubits. kernel.ch(controls=[controls[0], controls[1]], target=target)
- s()¶
- s(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a s gate to the given target qubit or qubits.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply s to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a s gate to the qubit/s. kernel.s(qubits)
- sdg()¶
- sdg(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a rotation on the z-axis of negative 90 degrees to the given target qubit/s.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply an sdg gate to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a sdg gate to the qubit/s. kernel.sdg(qubit)
- cs(*args, **kwargs)¶
Overloaded function.
- cs(self: cudaq.Kernel, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-s operation to the given target qubit, with the provided control qubit/s.
- Parameters:
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-s between the two qubits. kernel.cs(control=control, target=target)
- cs(self: cudaq.Kernel, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-s operation to the given target qubits, with the provided list of control qubits.
- Parameters:
controls (list[QuakeValue]) – The list of qubits to use as controls for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() controls = kernel.qalloc(2) target = kernel.qalloc() # Apply a controlled-s to the target qubit, with the two control qubits. kernel.cs(controls=[controls[0], controls[1]], target=target)
- t()¶
- t(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a t gate to the given target qubit or qubits.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply t to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a t gate to the qubit/s. kernel.t(qubits)
- tdg()¶
- tdg(self: cudaq.Kernel, target: cudaq.QuakeValue) None
Apply a rotation on the z-axis of negative 45 degrees to the given target qubit/s.
- Parameters:
target (
QuakeValue
) – The qubit or qubits to apply a tdg gate to.
# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to the `kernel`. qubits = kernel.qalloc(5) # Apply a tdg gate to the qubit/s. kernel.tdg(qubit)
- ct(*args, **kwargs)¶
Overloaded function.
- ct(self: cudaq.Kernel, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-t operation to the given target qubit, with the provided control qubit/s.
- Parameters:
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-t between the two qubits. kernel.ct(control=control, target=target)
- ct(self: cudaq.Kernel, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-t operation to the given target qubits, with the provided list of control qubits.
- Parameters:
controls (list[QuakeValue]) – The list of qubits to use as controls for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() controls = kernel.qalloc(2) target = kernel.qalloc() # Apply a controlled-t to the target qubit, with the two control qubits. kernel.ct(controls=[controls[0], controls[1]], target=target)
- rx(*args, **kwargs)¶
Overloaded function.
- rx(self: cudaq.Kernel, parameter: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply rx to the given target qubit, parameterized by the provided kernel argument (
parameter
).- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the rx gate over.target (
QuakeValue
) – The target qubit of the rx gate.
# Example: # Create a kernel that accepts a float, `angle`, as its argument. kernel, angle = cudaq.make_kernel(float) qubit = kernel.qalloc() # Apply an rx to the kernel at `angle`. kernel.rx(parameter=angle, target=qubit)
- rx(self: cudaq.Kernel, parameter: float, target: cudaq.QuakeValue) None
Apply rx to the given target qubit, parameterized by the provided float value (
parameter
).- Parameters:
parameter (float) – The float value to parameterize the rx gate over.
target (
QuakeValue
) – The target qubit of the rx gate.
# Example: kernel = cudaq.make_kernel() # Apply an rx to the kernel at a concrete parameter value. kernel.rx(parameter=3.14, target=qubit)
- crx(*args, **kwargs)¶
Overloaded function.
- crx(self: cudaq.Kernel, parameter: cudaq.QuakeValue, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-rx operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the rx gate over.control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rx between the qubits. kernel.crx(parameter=angle, control=control, target=target)
- crx(self: cudaq.Kernel, parameter: float, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-rx operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (float) – The float value to parameterize the rx gate over.
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rx between the two qubits. kernel.crx(parameter=3.14, control=control, target=target)
- crx(self: cudaq.Kernel, parameter: cudaq.QuakeValue, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-rx operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The kernel argument to parameterize the rx gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rx between the qubits. kernel.crx(parameter=angle, controls=[c1, c2], target=target)
- crx(self: cudaq.Kernel, parameter: float, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-rx operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The float value to parameterize the rx gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rx between the qubits. kernel.crx(parameter=3.14, controls=[c1, c2], target=target)
- ry(*args, **kwargs)¶
Overloaded function.
- ry(self: cudaq.Kernel, parameter: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply ry to the given target qubit, parameterized by the provided kernel argument (
parameter
).- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the ry gate over.target (
QuakeValue
) – The target qubit of the ry gate.
# Example: # Create a kernel that accepts a float, `angle`, as its argument. kernel, angle = cudaq.make_kernel(float) qubit = kernel.qalloc() # Apply an ry to the kernel at `angle`. kernel.ry(parameter=angle, target=qubit)
- ry(self: cudaq.Kernel, parameter: float, target: cudaq.QuakeValue) None
Apply ry to the given target qubit, parameterized by the provided float value (
parameter
).- Parameters:
parameter (float) – The float value to parameterize the ry gate over.
target (
QuakeValue
) – The target qubit of the ry gate.
# Example: kernel = cudaq.make_kernel() # Apply an ry to the kernel at a concrete parameter value. kernel.ry(parameter=3.14, target=qubit)
- cry(*args, **kwargs)¶
Overloaded function.
- cry(self: cudaq.Kernel, parameter: cudaq.QuakeValue, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-ry operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the ry gate over.control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-ry between the qubits. kernel.cry(parameter=angle, control=control, target=target)
- cry(self: cudaq.Kernel, parameter: float, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-ry operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (float) – The float value to parameterize the ry gate over.
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-ry between the two qubits. kernel.cry(parameter=3.14, control=control, target=target)
- cry(self: cudaq.Kernel, parameter: cudaq.QuakeValue, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-ry operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The kernel argument to parameterize the ry gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-ry between the qubits. kernel.cry(parameter=angle, controls=[c1, c2], target=target)
- cry(self: cudaq.Kernel, parameter: float, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-ry operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The float value to parameterize the ry gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-ry between the qubits. kernel.cry(parameter=3.14, controls=[c1, c2], target=target)
- rz(*args, **kwargs)¶
Overloaded function.
- rz(self: cudaq.Kernel, parameter: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply rz to the given target qubit, parameterized by the provided kernel argument (
parameter
).- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the rz gate over.target (
QuakeValue
) – The target qubit of the rz gate.
# Example: # Create a kernel that accepts a float, `angle`, as its argument. kernel, angle = cudaq.make_kernel(float) qubit = kernel.qalloc() # Apply an rz to the kernel at `angle`. kernel.rz(parameter=angle, target=qubit)
- rz(self: cudaq.Kernel, parameter: float, target: cudaq.QuakeValue) None
Apply rz to the given target qubit, parameterized by the provided float value (
parameter
).- Parameters:
parameter (float) – The float value to parameterize the rz gate over.
target (
QuakeValue
) – The target qubit of the rz gate.
# Example: kernel = cudaq.make_kernel() # Apply an rz to the kernel at a concrete parameter value. kernel.rz(parameter=3.14, target=qubit)
- crz(*args, **kwargs)¶
Overloaded function.
- crz(self: cudaq.Kernel, parameter: cudaq.QuakeValue, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-rz operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the rz gate over.control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rz between the qubits. kernel.crz(parameter=angle, control=control, target=target)
- crz(self: cudaq.Kernel, parameter: float, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-rz operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (float) – The float value to parameterize the rz gate over.
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rz between the two qubits. kernel.crz(parameter=3.14, control=control, target=target)
- crz(self: cudaq.Kernel, parameter: cudaq.QuakeValue, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-rz operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The kernel argument to parameterize the rz gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rz between the qubits. kernel.crz(parameter=angle, controls=[c1, c2], target=target)
- crz(self: cudaq.Kernel, parameter: float, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-rz operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The float value to parameterize the rz gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-rz between the qubits. kernel.crz(parameter=3.14, controls=[c1, c2], target=target)
- r1(*args, **kwargs)¶
Overloaded function.
- r1(self: cudaq.Kernel, parameter: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply r1 to the given target qubit, parameterized by the provided kernel argument (
parameter
).- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the r1 gate over.target (
QuakeValue
) – The target qubit of the r1 gate.
# Example: # Create a kernel that accepts a float, `angle`, as its argument. kernel, angle = cudaq.make_kernel(float) qubit = kernel.qalloc() # Apply an r1 to the kernel at `angle`. kernel.r1(parameter=angle, target=qubit)
- r1(self: cudaq.Kernel, parameter: float, target: cudaq.QuakeValue) None
Apply r1 to the given target qubit, parameterized by the provided float value (
parameter
).- Parameters:
parameter (float) – The float value to parameterize the r1 gate over.
target (
QuakeValue
) – The target qubit of the r1 gate.
# Example: kernel = cudaq.make_kernel() # Apply an r1 to the kernel at a concrete parameter value. kernel.r1(parameter=3.14, target=qubit)
- cr1(*args, **kwargs)¶
Overloaded function.
- cr1(self: cudaq.Kernel, parameter: cudaq.QuakeValue, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-r1 operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (
QuakeValue
) – The kernel argument to parameterize the r1 gate over.control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-r1 between the qubits. kernel.cr1(parameter=angle, control=control, target=target)
- cr1(self: cudaq.Kernel, parameter: float, control: cudaq.QuakeValue, target: cudaq.QuakeValue) None
Apply a controlled-r1 operation to the given target qubit, with the provided control qubit/s.
- Parameters:
parameter (float) – The float value to parameterize the r1 gate over.
control (
QuakeValue
) – The control qubit/s for the operation.target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() # Our `control` may either be a single qubit or a register of qubits. control = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-r1 between the two qubits. kernel.cr1(parameter=3.14, control=control, target=target)
- cr1(self: cudaq.Kernel, parameter: cudaq.QuakeValue, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-r1 operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The kernel argument to parameterize the r1 gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel, angle = cudaq.make_kernel(float) c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-r1 between the qubits. kernel.cr1(parameter=angle, controls=[c1, c2], target=target)
- cr1(self: cudaq.Kernel, parameter: float, controls: List[cudaq.QuakeValue], target: cudaq.QuakeValue) None
Apply a controlled-r1 operation to the given target qubit, with the provided list of control qubits.
- Parameters:
parameter (float) – The float value to parameterize the r1 gate over.
controls (list[QuakeValue]) – The control qubits for the operation.
target (
QuakeValue
) – The target qubit of the operation.
# Example: kernel = cudaq.make_kernel() c1 = kernel.qalloc() c2 = kernel.qalloc() target = kernel.qalloc() # Apply a controlled-r1 between the qubits. kernel.cr1(parameter=3.14, controls=[c1, c2], target=target)
- swap()¶
- swap(self: cudaq.Kernel, first: cudaq.QuakeValue, second: cudaq.QuakeValue) None
Swap the states of the provided qubits.
- Parameters:
first (
QuakeValue
) – The target qubit of the operation. Its state will be swapped with thesecond
qubit.second (
QuakeValue
) – The target qubit of the operation. Its state will be swapped with thefirst
qubit.
# Example: kernel = cudaq.make_kernel() first = kernel.qalloc() second = kernel.qalloc() # Place `first` in the |1> state, and leave `second` in |0>. kernel.x(first) # SWAP their states, resulting in the transformation: `|10> -> |01>`. kernel.swap(first, second)
- cswap(*args, **kwargs)¶
Overloaded function.
- cswap(self: cudaq.Kernel, control: cudaq.QuakeValue, first: cudaq.QuakeValue, second: cudaq.QuakeValue) None
Perform a controlled-SWAP operation between two qubits, if and only if the state of the provided
control
qubit/s is 1.- Parameters:
control (
QuakeValue
) – The control qubit/s for the operation.first (
QuakeValue
) – The target qubit of the operation. Its state will swap with thesecond
qubit, based on the state of the inputcontrol
.second (
QuakeValue
) – The target qubit of the operation. Its state will swap with thefirst
qubit, based on the state of the inputcontrol
.
# Example: kernel = cudaq.make_kernel() # Allocate control qubit/s to the `kernel`. control = kernel.qalloc(2) # Allocate target qubits to the `kernel`. targets = kernel.qalloc(2) # Place the 0th target qubit in the 1-state. kernel.x(targets[0]) # Place our control/s in the 1-state. kernel.x(control) # Since the `control` is all in the 1-state, our target # states should undergo a SWAP. kernel.cswap(control, targets[0], targets[1])
- cswap(self: cudaq.Kernel, controls: List[cudaq.QuakeValue], first: cudaq.QuakeValue, second: cudaq.QuakeValue) None
Perform a controlled-SWAP operation between two qubits, if and only if the states of all provided
control
qubits are 1.- Parameters:
controls (list[QuakeValue]) – The list of control qubits for the operation.
first (
QuakeValue
) – The target qubit of the operation. Its state will swap with thesecond
qubit, based on the state of the inputcontrols
.second (
QuakeValue
) – The target qubit of the operation. Its state will swap with thefirst
qubit, based on the state of the inputcontrols
.
# Example: kernel = cudaq.make_kernel() # Allocate control qubits to the `kernel`. controls = [kernel.qalloc(), kernel.qalloc()] # Allocate target qubits to the `kernel`. targets = kernel.qalloc(2) # Place the 0th target qubit in the 1-state. kernel.x(targets[0]) # Place our controls in the 1-state. kernel.x(controls[0]) kernel.x(controls[1]) # Since the `controls` are all in the 1-state, our target # states should undergo a SWAP. kernel.cswap(controls, targets[0], targets[1])
- exp_pauli()¶
- exp_pauli(self: cudaq.Kernel, arg0: object, arg1: cudaq.QuakeValue, arg2: Union[str, cudaq.SpinOperator]) None
Apply a general Pauli tensor product rotation,
exp(i theta P)
, on the specified qubit register. The Pauli tensor product is provided as a string, e.g.XXYX
for a 4-qubit term. The angle parameter can be provided as a concrete float or aQuakeValue
.
- mx()¶
- mx(self: cudaq.Kernel, target: cudaq.QuakeValue, register_name: str = '') cudaq.QuakeValue
Measure the given qubit or qubits in the X-basis. The optional
register_name
may be used to retrieve results of this measurement after execution on the QPU. If the measurement call is saved as a variable, it will return aQuakeValue
handle to the measurement instruction.- Parameters:
target (
QuakeValue
) – The qubit or qubits to measure.register_name (Optional[str]) – The optional name to provide the results of the measurement. Defaults to an empty string.
- Returns:
A handle to this measurement operation in the MLIR.
- Return type:
Note
Measurements may be applied both mid-circuit and at the end of the circuit. Mid-circuit measurements are currently only supported through the use of
c_if()
.# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to measure. qubit = kernel.qalloc() # Measure the qubit/s in the X-basis. kernel.mx(qubit)
- my()¶
- my(self: cudaq.Kernel, target: cudaq.QuakeValue, register_name: str = '') cudaq.QuakeValue
Measure the given qubit or qubits in the Y-basis. The optional
register_name
may be used to retrieve results of this measurement after execution on the QPU. If the measurement call is saved as a variable, it will return aQuakeValue
handle to the measurement instruction.- Parameters:
target (
QuakeValue
) – The qubit or qubits to measure.register_name (Optional[str]) – The optional name to provide the results of the measurement. Defaults to an empty string.
- Returns:
A handle to this measurement operation in the MLIR.
- Return type:
Note
Measurements may be applied both mid-circuit and at the end of the circuit. Mid-circuit measurements are currently only supported through the use of
c_if()
.# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to measure. qubit = kernel.qalloc() # Measure the qubit/s in the Y-basis. kernel.my(qubit)
- mz()¶
- mz(self: cudaq.Kernel, target: cudaq.QuakeValue, register_name: str = '') cudaq.QuakeValue
Measure the given qubit or qubits in the Z-basis. The optional
register_name
may be used to retrieve results of this measurement after execution on the QPU. If the measurement call is saved as a variable, it will return aQuakeValue
handle to the measurement instruction.- Parameters:
target (
QuakeValue
) – The qubit or qubits to measure.register_name (Optional[str]) – The optional name to provide the results of the measurement. Defaults to an empty string.
- Returns:
A handle to this measurement operation in the MLIR.
- Return type:
Note
Measurements may be applied both mid-circuit and at the end of the circuit. Mid-circuit measurements are currently only supported through the use of
c_if()
.# Example: kernel = cudaq.make_kernel() # Allocate qubit/s to measure. qubit = kernel.qalloc() # Measure the qubit/s in the Z-basis. kernel.mz(target=qubit)
- c_if()¶
- c_if(self: cudaq.Kernel, measurement: cudaq.QuakeValue, function: function) None
Apply the
function
to theKernel
if the provided single-qubitmeasurement
returns the 1-state.- Parameters:
measurement (
QuakeValue
) – The handle to the single qubit measurement instruction.function (Callable) – The function to conditionally apply to the
Kernel
.
- Raises:
RuntimeError – If the provided
measurement
is on more than 1 qubit.
# Example: # Create a kernel and allocate a single qubit. kernel = cudaq.make_kernel() qubit = kernel.qalloc() # Define a function that performs certain operations on the # kernel and the qubit. def then_function(): kernel.x(qubit) kernel.x(qubit) # Measure the qubit. measurement = kernel.mz(qubit) # Apply `then_function` to the `kernel` if the qubit was measured # in the 1-state. kernel.c_if(measurement, then_function)
- for_loop(*args, **kwargs)¶
Overloaded function.
- for_loop(self: cudaq.Kernel, start: int, stop: int, function: function) None
Add a for loop that starts from the given
start
integer index, ends at the givenstop
integer index (non inclusive), applying the providedfunction
withinself
at each iteration.- Parameters:
Note
This callable function must take as input an index variable that can be used within the body.
# Example: # Create a kernel and allocate (5) qubits to it. kernel = cudaq.make_kernel() qubits = kernel.qalloc(5) kernel.h(qubits[0]) def foo(index: int): """A function that will be applied to `kernel` in a for loop.""" kernel.cx(qubits[index], qubits[index+1]) # Create a for loop in `kernel`, providing a concrete number # of iterations to run (4). kernel.for_loop(start=0, stop=4, function=foo) # Execute the kernel. result = cudaq.sample(kernel) print(result)
- for_loop(self: cudaq.Kernel, start: int, stop: cudaq.QuakeValue, function: function) None
Add a for loop that starts from the given
start
integer index, ends at the givenstop
QuakeValue
index (non inclusive), applying the providedfunction
withinself
at each iteration.- Parameters:
start (int) – The beginning iterator value for the for loop.
stop (
QuakeValue
) – The final iterator value (non-inclusive) for the for loop.function (Callable) – The callable function to apply within the
kernel
at each iteration.
# Example: # Create a kernel function that takes an `int` argument. kernel, size = cudaq.make_kernel(int) # Parameterize the allocated number of qubits by the int. qubits = kernel.qalloc(size) kernel.h(qubits[0]) def foo(index: int): """A function that will be applied to `kernel` in a for loop.""" kernel.cx(qubits[index], qubits[index+1]) # Create a for loop in `kernel`, parameterized by the `size` # argument for its `stop` iterator. kernel.for_loop(start=0, stop=size-1, function=foo) # Execute the kernel, passing along a concrete value (5) for # the `size` argument. counts = cudaq.sample(kernel, 5) print(counts)
- for_loop(self: cudaq.Kernel, start: cudaq.QuakeValue, stop: int, function: function) None
Add a for loop that starts from the given
start
QuakeValue
index, ends at the givenstop
integer index (non inclusive), applying the providedfunction
withinself
at each iteration.- Parameters:
start (
QuakeValue
) – The beginning iterator value for the for loop.stop (int) – The final iterator value (non-inclusive) for the for loop.
function (Callable) – The callable function to apply within the
kernel
at each iteration.
# Example: # Create a kernel function that takes an `int` argument. kernel, start = cudaq.make_kernel(int) # Allocate 5 qubits. qubits = kernel.qalloc(5) kernel.h(qubits[0]) def foo(index: int): """A function that will be applied to `kernel` in a for loop.""" kernel.cx(qubits[index], qubits[index+1]) # Create a for loop in `kernel`, with its start index being # parameterized by the kernel's `start` argument. kernel.for_loop(start=start, stop=4, function=foo) # Execute the kernel, passing along a concrete value (0) for # the `start` argument. counts = cudaq.sample(kernel, 0) print(counts)
- for_loop(self: cudaq.Kernel, start: cudaq.QuakeValue, stop: cudaq.QuakeValue, function: function) None
Add a for loop that starts from the given
start
QuakeValue
index, and ends at the givenstop
QuakeValue
index (non inclusive). The providedfunction
will be applied withinself
at each iteration.- Parameters:
start (
QuakeValue
) – The beginning iterator value for the for loop.stop (
QuakeValue
) – The final iterator value (non-inclusive) for the for loop.function (Callable) – The callable function to apply within the
kernel
at each iteration.
# Example: # Create a kernel function that takes two `int`'s as arguments. kernel, start, stop = cudaq.make_kernel(int, int) # Parameterize the allocated number of qubits by the int. qubits = kernel.qalloc(stop) kernel.h(qubits[0]) def foo(index: int): """A function that will be applied to `kernel` in a for loop.""" kernel.x(qubits[index]) # Create a for loop in `kernel`, parameterized by the `start` # and `stop` arguments. kernel.for_loop(start=start, stop=stop, function=foo) # Execute the kernel, passing along concrete values for the # `start` and `stop` arguments. counts = cudaq.sample(kernel, 3, 8) print(counts)
- adjoint()¶
- adjoint(self: cudaq.Kernel, target: cudaq.Kernel, \*args) None
Apply the adjoint of the
target
kernel in-place toself
.- Parameters:
target (
Kernel
) – The kernel to take the adjoint of.*target_arguments (Optional[QuakeValue]) – The arguments to the
target
kernel. Leave empty if thetarget
kernel doesn’t accept any arguments.
- Raises:
RuntimeError – if the
*target_arguments
passed to the adjoint call don’t match the argument signature oftarget
.
# Example: target_kernel = cudaq.make_kernel() qubit = target_kernel.qalloc() target_kernel.x(qubit) # Apply the adjoint of `target_kernel` to `kernel`. kernel = cudaq.make_kernel() kernel.adjoint(target_kernel)
- control()¶
- control(self: cudaq.Kernel, target: cudaq.Kernel, control: cudaq.QuakeValue, \*args) None
Apply the
target
kernel as a controlled operation in-place toself
.Uses the providedcontrol
as control qubit/s for the operation.- Parameters:
target (
Kernel
) – The kernel to apply as a controlled operation in-place to self.control (
QuakeValue
) – The control qubit or register to use when applyingtarget
.*target_arguments (Optional[QuakeValue]) – The arguments to the
target
kernel. Leave empty if thetarget
kernel doesn’t accept any arguments.
- Raises:
RuntimeError – if the
*target_arguments
passed to the control call don’t match the argument signature oftarget
.
# Example: # Create a `Kernel` that accepts a qubit as an argument. # Apply an X-gate on that qubit. target_kernel, qubit = cudaq.make_kernel(cudaq.qubit) target_kernel.x(qubit) # Create another `Kernel` that will apply `target_kernel` # as a controlled operation. kernel = cudaq.make_kernel() control_qubit = kernel.qalloc() target_qubit = kernel.qalloc() # In this case, `control` performs the equivalent of a # controlled-X gate between `control_qubit` and `target_qubit`. kernel.control(target_kernel, control_qubit, target_qubit)
- apply_call()¶
- apply_call(self: cudaq.Kernel, target: cudaq.Kernel, \*args) None
Apply a call to the given
target
kernel within the function-body ofself
at the providedtarget_arguments
.- Parameters:
target (
Kernel
) – The kernel to call from withinself
.*target_arguments (Optional[QuakeValue]) – The arguments to the
target
kernel. Leave empty if thetarget
kernel doesn’t accept any arguments.
- Raises:
RuntimeError – if the
*args
passed to the apply call don’t match the argument signature oftarget
.
# Example: # Build a `Kernel` that's parameterized by a `cudaq.qubit`. target_kernel, other_qubit = cudaq.make_kernel(cudaq.qubit) target_kernel.x(other_qubit) # Build a `Kernel` that will call `target_kernel` within its # own function body. kernel = cudaq.make_kernel() qubit = kernel.qalloc() # Use `qubit` as the argument to `target_kernel`. kernel.apply_call(target_kernel, qubit) # The final measurement of `qubit` should return the 1-state. kernel.mz(qubit)
Kernel Execution¶
- cudaq.sample()¶
- cudaq.sample(kernel: cudaq.Kernel, \*args, shots_count: int = 1000, noise_model: Optional[cudaq.NoiseModel] = None) Union[cudaq.SampleResult, List[cudaq.SampleResult]]
Sample the state generated by the provided
kernel
at the given kernelarguments
over the specified number of circuit executions (shots_count
). Each argument inarguments
provided can be a list or ndarray of arguments of the specified kernel argument type, and in this case, thesample
functionality will be broadcasted over all argument sets and a list ofsample_result
instances will be returned.- Parameters:
kernel (
Kernel
) – TheKernel
to executeshots_count
times on the QPU.*arguments (Optional[Any]) – The concrete values to evaluate the kernel function at. Leave empty if the kernel doesn’t accept any arguments. For example, if the kernel takes two
float
values as input, thesample
call should be structured ascudaq.sample(kernel, firstFloat, secondFloat)
. For broadcasting of thesample
function, the arguments should be structured as alist
orndarray
of argument values of the specified kernel argument type.shots_count (Optional[int]) – The number of kernel executions on the QPU. Defaults to 1000. Key-word only.
noise_model (Optional[
NoiseModel
]) – The optionalNoiseModel
to add noise to the kernel execution on the simulator. Defaults to an empty noise model.
- Returns:
A dictionary containing the measurement count results for the
Kernel
, or a list of such results in the case ofsample
function broadcasting.- Return type:
SampleResult
orlist[SampleResult]
- cudaq.sample_async()¶
- cudaq.sample_async(kernel: cudaq.Kernel, \*args, shots_count: int = 1000, qpu_id: int = 0) cudaq.AsyncSampleResult
Asynchronously sample the state of the provided
kernel
at the specified number of circuit executions (shots_count
). When targeting a quantum platform with more than one QPU, the optionalqpu_id
allows for control over which QPU to enable. Will return a future whose results can be retrieved viafuture.get()
.- Parameters:
kernel (
Kernel
) – TheKernel
to executeshots_count
times on the QPU.*arguments (Optional[Any]) – The concrete values to evaluate the kernel function at. Leave empty if the kernel doesn’t accept any arguments.
shots_count (Optional[int]) – The number of kernel executions on the QPU. Defaults to 1000. Key-word only.
qpu_id (Optional[int]) – The optional identification for which QPU on the platform to target. Defaults to zero. Key-word only.
- Returns:
A dictionary containing the measurement count results for the
Kernel
.- Return type:
- cudaq.observe()¶
- cudaq.observe(kernel: cudaq.Kernel, spin_operator: Union[cudaq.SpinOperator, List[cudaq.SpinOperator]], \*args, shots_count: int = -1, noise_model: Optional[cudaq.NoiseModel] = None, execution: Optional[type] = None) Union[cudaq.ObserveResult, List[cudaq.ObserveResult], List[List[cudaq.ObserveResult]]]
Compute the expected value of the
spin_operator
with respect to thekernel
. If the inputspin_operator
is a list ofSpinOperator
then compute the expected value of every operator in the list and return a list of results. If the kernel accepts arguments, it will be evaluated with respect tokernel(*arguments)
. Each argument inarguments
provided can be a list or ndarray of arguments of the specified kernel argument type, and in this case, theobserve
functionality will be broadcasted over all argument sets and a list ofobserve_result
instances will be returned. If both the inputspin_operator
andarguments
are broadcast lists, a nested list of results overarguments
thenspin_operator
will be returned.- Parameters:
kernel (
Kernel
) – TheKernel
to evaluate the expectation value with respect to.spin_operator (
SpinOperator
orlist[SpinOperator]
) – The Hermitian spin operator to calculate the expectation of, or a list of such operators.*arguments (Optional[Any]) – The concrete values to evaluate the kernel function at. Leave empty if the kernel doesn’t accept any arguments.
shots_count (Optional[int]) – The number of shots to use for QPU execution. Defaults to -1 implying no shots-based sampling. Key-word only.
noise_model (Optional[
NoiseModel
]) – The optionalNoiseModel
to add noise to the kernel execution on the simulator. Defaults to an empty noise model.
- Returns:
A data-type containing the expectation value of the
spin_operator
with respect to thekernel(*arguments)
, or a list of such results in the case ofobserve
function broadcasting. Ifshots_count
was provided, theObserveResult
will also contain aSampleResult
dictionary.- Return type:
- cudaq.observe_async()¶
- cudaq.observe_async(kernel: cudaq.Kernel, spin_operator: cudaq.SpinOperator, \*args, qpu_id: int = 0, shots_count: int = -1, noise_model: Optional[cudaq.NoiseModel] = None) cudaq.AsyncObserveResult
Compute the expected value of the
spin_operator
with respect to thekernel
asynchronously. If the kernel accepts arguments, it will be evaluated with respect tokernel(*arguments)
. When targeting a quantum platform with more than one QPU, the optionalqpu_id
allows for control over which QPU to enable. Will return a future whose results can be retrieved viafuture.get()
.- Parameters:
kernel (
Kernel
) – TheKernel
to evaluate the expectation value with respect to.spin_operator (
SpinOperator
) – The Hermitian spin operator to calculate the expectation of.*arguments (Optional[Any]) – The concrete values to evaluate the kernel function at. Leave empty if the kernel doesn’t accept any arguments.
qpu_id (Optional[int]) – The optional identification for which QPU on the platform to target. Defaults to zero. Key-word only.
shots_count (Optional[int]) – The number of shots to use for QPU execution. Defaults to -1 implying no shots-based sampling. Key-word only.
noise_model (Optional[
NoiseModel
]) – The optionalNoiseModel
to add noise to the kernel execution on the simulator. Defaults to an empty noise model.
- Returns:
A future containing the result of the call to observe.
- Return type:
- cudaq.get_state()¶
- cudaq.get_state(arg0: cudaq.Kernel, \*args) cudaq.State
Return the
State
of the system after execution of the providedkernel
.- Parameters:
# Example: import numpy as np # Define a kernel that will produced the all |11...1> state. kernel = cudaq.make_kernel() qubits = kernel.qalloc(3) # Prepare qubits in the 1-state. kernel.x(qubits) # Get the state of the system. This will execute the provided kernel # and, depending on the selected target, will return the state as a # vector or matrix. state = cudaq.get_state(kernel) print(state)
- cudaq.get_state_async()¶
- cudaq.get_state_async(kernel: cudaq.Kernel, \*args, qpu_id: int = 0) cudaq.AsyncStateResult
Asynchronously retrieve the state generated by the given quantum kernel. When targeting a quantum platform with more than one QPU, the optional
qpu_id
allows for control over which QPU to enable. Will return a future whose results can be retrieved viafuture.get()
.- Parameters:
*arguments (Optional[Any]) – The concrete values to evaluate the kernel function at. Leave empty if the kernel doesn’t accept any arguments.
qpu_id (Optional[int]) – The optional identification for which QPU on the platform to target. Defaults to zero. Key-word only.
- Returns:
Quantum state (state vector or density matrix) data).
- Return type:
- cudaq.vqe(*args, **kwargs)¶
Overloaded function.
- cudaq.vqe(kernel: cudaq.Kernel, spin_operator: cudaq.SpinOperator, optimizer: cudaq.optimizers.optimizer, parameter_count: int, shots: int = -1) Tuple[float, List[float]]
- cudaq.vqe(kernel: cudaq.Kernel, spin_operator: cudaq.SpinOperator, optimizer: cudaq.optimizers.optimizer, parameter_count: int, argument_mapper: function, shots: int = -1) Tuple[float, List[float]]
- cudaq.vqe(kernel: cudaq.Kernel, gradient_strategy: cudaq.gradients.gradient, spin_operator: cudaq.SpinOperator, optimizer: cudaq.optimizers.optimizer, parameter_count: int, shots: int = -1) Tuple[float, List[float]]
- cudaq.vqe(kernel: cudaq.Kernel, gradient_strategy: cudaq.gradients.gradient, spin_operator: cudaq.SpinOperator, optimizer: cudaq.optimizers.optimizer, parameter_count: int, argument_mapper: function, shots: int = -1) Tuple[float, List[float]]
Backend Configuration¶
- cudaq.has_target()¶
-
Return true if the
cudaq.Target
with the given name exists.
- cudaq.get_target(*args, **kwargs)¶
Overloaded function.
- cudaq.get_target(arg0: str) cudaq.Target
Return the
cudaq.Target
with the given name. Will raise an exception if the name is not valid.- cudaq.get_target() cudaq.Target
Return the
cudaq.Target
with the given name. Will raise an exception if the name is not valid.
- cudaq.get_targets()¶
- cudaq.get_targets() List[cudaq.Target]
Return all available
cudaq.Target
instances on the current system.
- cudaq.set_target(*args, **kwargs)¶
Overloaded function.
- cudaq.set_target(arg0: cudaq.Target, \*\*kwargs) None
Set the
cudaq.Target
to be used for CUDA Quantum kernel execution. Can provide optional, target-specific configuration data via Python kwargs.- cudaq.set_target(arg0: str, \*\*kwargs) None
Set the
cudaq.Target
with given name to be used for CUDA Quantum kernel execution. Can provide optional, target-specific configuration data via Python kwargs.
- cudaq.set_noise()¶
- cudaq.set_noise(arg0: cudaq.NoiseModel) None
Set the underlying noise model.
- cudaq.unset_noise()¶
- cudaq.unset_noise() None
Clear backend simulation from any existing noise models.
- cudaq.initialize_cudaq()¶
- cudaq.initialize_cudaq(\*\*kwargs) None
Initialize the CUDA Quantum environment.
Data Types¶
- class cudaq.Target¶
The
cudaq.Target
represents the underlying infrastructure that CUDA Quantum kernels will execute on. Instances ofcudaq.Target
describe what simulator they may leverage, the quantum_platform required for execution, and a description for the target.- property description¶
A string describing the features for this
cudaq.Target
.
- property name¶
The name of the
cudaq.Target
.
- num_qpus()¶
- num_qpus(self: cudaq.Target) int
Return the number of QPUs available in this
cudaq.Target
.
- property platform¶
The name of the quantum_platform implementation this
cudaq.Target
leverages.
- property simulator¶
The name of the simulator this
cudaq.Target
leverages. This will be empty for physical QPUs.
- class cudaq.State¶
A data-type representing the quantum state of the internal simulator. Returns a state vector by default. If the target is set to
density-matrix-cpu
, a density matrix will be returned.- dump()¶
- dump(self: cudaq.State) None
Print the state to the console.
- overlap(*args, **kwargs)¶
Overloaded function.
- overlap(self: cudaq.State, arg0: cudaq.State) float
Compute the overlap between the provided
State
’s.- overlap(self: cudaq.State, arg0: numpy.ndarray) float
Compute the overlap between the provided
State
’s.
- class cudaq.QuakeValue¶
A
QuakeValue
represents a handle to an individual function argument of aKernel
, or a return value from an operation within it. As documented inmake_kernel()
, aQuakeValue
can hold values of the following types: int, float, list/List,qubit
, orqreg
. TheQuakeValue
can also hold kernel operations such as qubit allocations and measurements.- __add__(*args, **kwargs)¶
Overloaded function.
- __add__(self: cudaq.QuakeValue, other: float) cudaq.QuakeValue
Return the sum of
self
(QuakeValue
) andother
(float).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, value = cudaq.make_kernel(float) new_value: QuakeValue = value + 5.0
- __add__(self: cudaq.QuakeValue, other: cudaq.QuakeValue) cudaq.QuakeValue
Return the sum of
self
(QuakeValue
) andother
(QuakeValue
).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not the same type asself
.
# Example: kernel, values = cudaq.make_kernel(list) new_value: QuakeValue = values[0] + values[1]
- __add__(self: cudaq.QuakeValue, other: int) cudaq.QuakeValue
Return the sum of
self
(QuakeValue
) andother
(int).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not anint
.
# Example: kernel, values = cudaq.make_kernel(list) new_value: QuakeValue = values[0] + 2
- __radd__()¶
- __radd__(self: cudaq.QuakeValue, other: float) cudaq.QuakeValue
Return the sum of
other
(float) andself
(QuakeValue
).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, value = cudaq.make_kernel(float) new_value: QuakeValue = 5.0 + value
- __sub__(*args, **kwargs)¶
Overloaded function.
- __sub__(self: cudaq.QuakeValue, other: float) cudaq.QuakeValue
Return the difference of
self
(QuakeValue
) andother
(float).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, value = cudaq.make_kernel(float) new_value: QuakeValue = value - 5.0
- __sub__(self: cudaq.QuakeValue, other: cudaq.QuakeValue) cudaq.QuakeValue
Return the difference of
self
(QuakeValue
) andother
(QuakeValue
).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not the same type asself
.
# Example: kernel, values = cudaq.make_kernel(list) new_value: QuakeValue = values[0] - values[1]
- __sub__(self: cudaq.QuakeValue, other: int) cudaq.QuakeValue
Return the difference of
self
(QuakeValue
) andother
(int).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a int.
# Example: kernel, values = cudaq.make_kernel(list) new_value: QuakeValue = values[0] - 2
- __rsub__()¶
- __rsub__(self: cudaq.QuakeValue, other: float) cudaq.QuakeValue
Return the difference of
other
(float) andself
(QuakeValue
).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, value = cudaq.make_kernel(float) new_value: QuakeValue = 5.0 - value
- __neg__()¶
- __neg__(self: cudaq.QuakeValue) cudaq.QuakeValue
Return the negation of
self
(QuakeValue
).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, value = cudaq.make_kernel(float) new_value: QuakeValue = -value
- __mul__(*args, **kwargs)¶
Overloaded function.
- __mul__(self: cudaq.QuakeValue, other: float) cudaq.QuakeValue
Return the product of
self
(QuakeValue
) withother
(float).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, value = cudaq.make_kernel(float) new_value: QuakeValue = value * 5.0
- __mul__(self: cudaq.QuakeValue, other: cudaq.QuakeValue) cudaq.QuakeValue
Return the product of
self
(QuakeValue
) withother
(QuakeValue
).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, values = cudaq.make_kernel(list) new_value: QuakeValue = values[0] * values[1]
- __rmul__()¶
- __rmul__(self: cudaq.QuakeValue, other: float) cudaq.QuakeValue
Return the product of
other
(float) withself
(QuakeValue
).- Raises:
RuntimeError – if the underlying
QuakeValue
type is not a float.
# Example: kernel, value = cudaq.make_kernel(float) new_value: QuakeValue = 5.0 * value
- __getitem__(*args, **kwargs)¶
Overloaded function.
- __getitem__(self: cudaq.QuakeValue, index: int) cudaq.QuakeValue
Return the element of
self
at the providedindex
.Note
Only
list
orqreg
typeQuakeValue
’s may be indexed.- Parameters:
index (int) – The element of
self
that you’d like to return.- Returns:
A new
QuakeValue
for theindex
-th element ofself
.- Return type:
- Raises:
RuntimeError – if
self
is a non-subscriptableQuakeValue
.
- __getitem__(self: cudaq.QuakeValue, index: cudaq.QuakeValue) cudaq.QuakeValue
Return the element of
self
at the providedindex
.Note
Only
list
orqreg
typeQuakeValue
’s may be indexed.- Parameters:
index (QuakeValue) – The element of
self
that you’d like to return.- Returns:
A new
QuakeValue
for theindex
-th element ofself
.- Return type:
- Raises:
RuntimeError – if
self
is a non-subscriptableQuakeValue
.
- slice()¶
- slice(self: cudaq.QuakeValue, start: int, count: int) cudaq.QuakeValue
Return a slice of the given
QuakeValue
as a newQuakeValue
.Note
The underlying
QuakeValue
must be alist
orveq
.- Parameters:
- Returns:
A new
QuakeValue
containing a slice ofself
from thestart
-th element to thestart + count
-th element.- Return type:
- class cudaq.qubit¶
The data-type representing a qubit argument to a
Kernel
function.# Example: kernel, qubit = cudaq.make_kernel(cudaq.qubit)
- class cudaq.qreg¶
The data-type representing a register of qubits as an argument to a
Kernel
function.# Example: kernel, qreg = cudaq.make_kernel(cudaq.qreg)
- class cudaq.ComplexMatrix¶
The
ComplexMatrix
is a thin wrapper around a matrix of complex<double> elements.- __getitem__(*args, **kwargs)¶
Overloaded function.
- __getitem__(self: cudaq.ComplexMatrix, arg0: int, arg1: int) complex
Return the matrix element at i, j.
- __getitem__(self: cudaq.ComplexMatrix, arg0: Tuple[int, int]) complex
Return the matrix element at i, j.
- __str__()¶
- __str__(self: cudaq.ComplexMatrix) str
Write this matrix to a string representation.
- minimal_eigenvalue()¶
- minimal_eigenvalue(self: cudaq.ComplexMatrix) complex
Return the lowest eigenvalue for this
ComplexMatrix
.
- class cudaq.SpinOperator¶
- __eq__()¶
- __eq__(self: cudaq.SpinOperator, other: cudaq.SpinOperator) bool
Return true if the two
SpinOperator
’s are equal. Equality does not consider the coefficients.
- __add__(*args, **kwargs)¶
Overloaded function.
- __add__(self: cudaq.SpinOperator, other: cudaq.SpinOperator) cudaq.SpinOperator
Add the given
SpinOperator
to this one and return result as a newSpinOperator
.- __add__(self: cudaq.SpinOperator, other: float) cudaq.SpinOperator
Add a double to the given
SpinOperator
and return result as a newSpinOperator
.
- __radd__()¶
- __radd__(self: cudaq.SpinOperator, other: float) cudaq.SpinOperator
Add a
SpinOperator
to the given double and return result as a newSpinOperator
.
- __sub__(*args, **kwargs)¶
Overloaded function.
- __sub__(self: cudaq.SpinOperator, other: cudaq.SpinOperator) cudaq.SpinOperator
Subtract the given
SpinOperator
from this one and return result as a newSpinOperator
.- __sub__(self: cudaq.SpinOperator, other: float) cudaq.SpinOperator
Subtract a double from the given
SpinOperator
and return result as a newSpinOperator
.
- __rsub__()¶
- __rsub__(self: cudaq.SpinOperator, other: float) cudaq.SpinOperator
Subtract a
SpinOperator
from the given double and return result as a newSpinOperator
.
- __mul__(*args, **kwargs)¶
Overloaded function.
- __mul__(self: cudaq.SpinOperator, other: cudaq.SpinOperator) cudaq.SpinOperator
Multiply the given
SpinOperator
’s together and return result as a newSpinOperator
.- __mul__(self: cudaq.SpinOperator, other: float) cudaq.SpinOperator
Multiply the
SpinOperator
by the given double and return result as a newSpinOperator
.- __mul__(self: cudaq.SpinOperator, other: complex) cudaq.SpinOperator
Multiply the
SpinOperator
by the given complex value and return result as a newSpinOperator
.
- __rmul__(*args, **kwargs)¶
Overloaded function.
- __rmul__(self: cudaq.SpinOperator, other: float) cudaq.SpinOperator
Multiply the double by the given
SpinOperator
and return result as a newSpinOperator
.- __rmul__(self: cudaq.SpinOperator, other: complex) cudaq.SpinOperator
Multiply the complex value by the given
SpinOperator
and return result as a newSpinOperator
.
- __iter__()¶
- __iter__(self: cudaq.SpinOperator) Iterator
Loop through each term of this
SpinOperator
.
- distribute_terms()¶
- distribute_terms(self: cudaq.SpinOperator, chunk_count: int) List[cudaq.SpinOperator]
Return a list of
SpinOperator
representing a distribution of the terms in thisSpinOperator
intochunk_count
sized chunks.
- dump()¶
- dump(self: cudaq.SpinOperator) None
Print a string representation of this
SpinOperator
.
- for_each_pauli()¶
- for_each_pauli(self: cudaq.SpinOperator, function: function) None
For a single
SpinOperator
term, apply the given function to each pauli element in the term. The function must havevoid(pauli, int)
signature wherepauli
is the Pauli matrix type and theint
is the qubit index.
- for_each_term()¶
- for_each_term(self: cudaq.SpinOperator, function: function) None
Apply the given function to all terms in this
SpinOperator
. The input function must havevoid(SpinOperator)
signature.
- static from_word()¶
- from_word(word: str) cudaq.SpinOperator
Return a
SpinOperator
corresponding to the provided Pauliword
.# Example: # The first and third qubits will receive a Pauli X, # while the second qubit will receive a Pauli Y. word = "XYX" # Convert word to spin operator. spin_operator = cudaq.SpinOperator.from_word(word) print(spin_operator) # prints: `[1+0j] XYX`
- get_coefficient()¶
- get_coefficient(self: cudaq.SpinOperator) complex
Return the coefficient of this
SpinOperator
. Must be aSpinOperator
with one term, otherwise an exception is thrown.
- get_qubit_count()¶
- get_qubit_count(self: cudaq.SpinOperator) int
Return the number of qubits this
SpinOperator
is on.
- get_raw_data()¶
- get_raw_data(self: cudaq.SpinOperator) Tuple[List[List[bool]], List[complex]]
Return the raw data of this
SpinOperator
.
- get_term_count()¶
- get_term_count(self: cudaq.SpinOperator) int
Return the number of terms in this
SpinOperator
.
- is_identity()¶
- is_identity(self: cudaq.SpinOperator) bool
Returns a bool indicating if this
SpinOperator
is equal to the identity.
- static random()¶
- random(qubit_count: int, term_count: int, seed: int = 354125263) cudaq.SpinOperator
Return a random
SpinOperator
on the given number of qubits (qubit_count
) and composed of the given number of terms (term_count
). An optional seed value may also be provided.
- serialize()¶
- serialize(self: cudaq.SpinOperator) List[float]
Return a serialized representation of the
SpinOperator
. Specifically, this encoding is via a vector of doubles. The encoding is as follows: for each term, a list of doubles where the ith element is a 3.0 for a Y, a 1.0 for a X, and a 2.0 for a Z on qubit i, followed by the real and imaginary part of the coefficient. Each term is appended to the array forming one large 1d array of doubles. The array is ended with the total number of terms represented as a double.
- to_matrix()¶
- to_matrix(self: cudaq.SpinOperator) cudaq.ComplexMatrix
Return
self
as aComplexMatrix
.
- to_sparse_matrix()¶
- to_sparse_matrix(self: cudaq.SpinOperator) Tuple[List[complex], List[int], List[int]]
Return
self
as a sparse matrix. This representation is aTuple[list[complex], list[int], list[int]]
, encoding the non-zero values, rows, and columns of the matrix. This format is supported byscipy.sparse.csr_array
.
- to_string()¶
- to_string(self: cudaq.SpinOperator) str
Return a string representation of this
SpinOperator
.
- spin.i()¶
- i(target: int) cudaq.SpinOperator
Return an identity
SpinOperator
on the given target qubit index.
- spin.x()¶
- x(target: int) cudaq.SpinOperator
Return an X
SpinOperator
on the given target qubit index.
- spin.y()¶
- y(target: int) cudaq.SpinOperator
Return a Y
SpinOperator
on the given target qubit index.
- spin.z()¶
- z(target: int) cudaq.SpinOperator
Return a Z
SpinOperator
on the given target qubit index.
- class cudaq.SampleResult¶
A data-type containing the results of a call to
sample()
. This includes all measurement counts data from both mid-circuit and terminal measurements.Note
At this time, mid-circuit measurements are not directly supported. Mid-circuit measurements may only be used if they are passed through to
c_if
.- register_names¶
A list of the names of each measurement register that are stored in
self
.- Type:
List[str]
- __getitem__()¶
- __getitem__(self: cudaq.SampleResult, bitstring: str) int
Return the measurement counts for the given
bitstring
.
- __iter__()¶
- __iter__(self: cudaq.SampleResult) Iterator
Iterate through the
SampleResult
dictionary.
- __len__()¶
- __len__(self: cudaq.SampleResult) int
Return the number of elements in
self
. Equivalent to the number of uniquely measured bitstrings.
- clear()¶
- clear(self: cudaq.SampleResult) None
Clear out all metadata from
self
.
- count()¶
- count(self: cudaq.SampleResult, bitstring: str, register_name: str = '__global__') int
Return the number of times the given bitstring was observed.
- Parameters:
- Returns:
The number of times the given bitstring was measured during the experiment.
- Return type:
- dump()¶
- dump(self: cudaq.SampleResult) None
Print a string of the raw measurement counts data to the terminal.
- expectation()¶
- expectation(self: cudaq.SampleResult, register_name: str = '__global__') float
Return the expectation value in the Z-basis of the
Kernel
that was sampled.
- expectation_z()¶
- expectation_z(self: cudaq.SampleResult, register_name: str = '__global__') float
Return the expectation value in the Z-basis of the
Kernel
that was sampled.
- get_marginal_counts()¶
- get_marginal_counts(self: cudaq.SampleResult, marginal_indices: List[int], \*, register_name: str = '__global__') cudaq.SampleResult
Extract the measurement counts data for the provided subset of qubits (
marginal_indices
).- Parameters:
- Returns:
A new
SampleResult
dictionary containing the extracted measurement data.- Return type:
- get_register_counts()¶
- get_register_counts(self: cudaq.SampleResult, register_name: str) cudaq.SampleResult
Extract the provided sub-register (
register_name
) as a newSampleResult
.
- get_sequential_data()¶
- get_sequential_data(self: cudaq.SampleResult, register_name: str = '__global__') List[str]
Return the data from the given register (
register_name
) as it was collected sequentially. A list of measurement results, not collated into a map.
- items()¶
- items(self: cudaq.SampleResult) Iterator
Return the key/value pairs in this
SampleResult
dictionary.
- most_probable()¶
- most_probable(self: cudaq.SampleResult, register_name: str = '__global__') str
Return the bitstring that was measured most frequently in the experiment.
- probability()¶
- probability(self: cudaq.SampleResult, bitstring: str, register_name: str = '__global__') float
Return the probability of measuring the given
bitstring
.- Parameters:
- Returns:
The probability of measuring the given
bitstring
. Equivalent to the proportion of the total times the bitstring was measured vs. the number of experiments (shots_count
).- Return type:
- values()¶
- values(self: cudaq.SampleResult) Iterator
Return all values (the counts) in this
SampleResult
dictionary.
- class cudaq.AsyncSampleResult¶
A data-type containing the results of a call to
sample_async()
. TheAsyncSampleResult
models a future-like type, whoseSampleResult
may be returned via an invocation of theget
method. This kicks off a wait on the current thread until the results are available. See future for more information on this programming pattern.- get()¶
- get(self: cudaq.AsyncSampleResult) cudaq.SampleResult
Return the
SampleResult
from the asynchronous sample execution.
- class cudaq.ObserveResult¶
A data-type containing the results of a call to
observe()
. This includes any measurement counts data, as well as the global expectation value of the user-definedspin_operator
.- counts(*args, **kwargs)¶
Overloaded function.
- counts(self: cudaq.ObserveResult) cudaq.SampleResult
Returns a
SampleResult
dictionary with the measurement results from the experiment. The result for each individual term of thespin_operator
is stored in its own measurement register. Each register name corresponds to the string representation of the spin term (without any coefficients).- counts(self: cudaq.ObserveResult, sub_term: cudaq.SpinOperator) cudaq.SampleResult
Given a
sub_term
of the globalspin_operator
that was passed toobserve()
, return its measurement counts.- Parameters:
sub_term (
SpinOperator
) – An individual sub-term of thespin_operator
.- Returns:
The measurement counts data for the individual
sub_term
.- Return type:
- dump()¶
- dump(self: cudaq.ObserveResult) None
Dump the raw data from the
SampleResult
that are stored inObserveResult
to the terminal.
- expectation(*args, **kwargs)¶
Overloaded function.
- expectation(self: cudaq.ObserveResult) float
Return the expectation value of the
spin_operator
that was provided inobserve()
.- expectation(self: cudaq.ObserveResult, sub_term: cudaq.SpinOperator) float
Return the expectation value of an individual
sub_term
of the globalspin_operator
that was passed toobserve()
.- Parameters:
sub_term (
SpinOperator
) – An individual sub-term of thespin_operator
.- Returns:
The expectation value of the
sub_term
with respect to theKernel
that was passed toobserve()
.- Return type:
- expectation_z(*args, **kwargs)¶
Overloaded function.
- expectation_z(self: cudaq.ObserveResult) float
Return the expectation value of the
spin_operator
that was provided inobserve()
.Note
expectation_z
has been deprecated in favor ofexpectation
.- expectation_z(self: cudaq.ObserveResult, sub_term: cudaq.SpinOperator) float
Return the expectation value of an individual
sub_term
of the globalspin_operator
that was passed toobserve()
.Note
expectation_z
has been deprecated in favor ofexpectation
.- Parameters:
sub_term (
SpinOperator
) – An individual sub-term of thespin_operator
.- Returns:
The expectation value of the
sub_term
with respect to theKernel
that was passed toobserve()
.- Return type:
- get_spin()¶
- get_spin(self: cudaq.ObserveResult) cudaq.SpinOperator
Return the
SpinOperator
corresponding to thisObserveResult
.
- class cudaq.AsyncObserveResult¶
A data-type containing the results of a call to
observe_async()
.The
AsyncObserveResult
contains a future, whoseObserveResult
may be returned via an invocation of theget
method.This kicks off a wait on the current thread until the results are available.
See future for more information on this programming pattern.
- get()¶
- get(self: cudaq.AsyncObserveResult) cudaq.ObserveResult
Returns the
ObserveResult
from the asynchronous observe execution.
- class cudaq.AsyncStateResult¶
A data-type containing the results of a call to
get_state_async()
. TheAsyncStateResult
models a future-like type, whoseState
may be returned via an invocation of theget
method. This kicks off a wait on the current thread until the results are available. See future for more information on this programming pattern.- get()¶
- get(self: cudaq.AsyncStateResult) cudaq.State
Return the
State
from the asynchronousget_state
accessor execution.
- class cudaq.OptimizationResult¶
Optimizers¶
- class cudaq.optimizers.optimizer¶
- class cudaq.optimizers.GradientDescent¶
- property initial_parameters¶
Set the initial parameter values for the optimization.
- property lower_bounds¶
Set the lower value bound for the optimization parameters.
- property max_iterations¶
Set the maximum number of optimizer iterations.
- optimize()¶
- optimize(self: cudaq.optimizers.GradientDescent, dimensions: int, function: function) Tuple[float, List[float]]
Run
cudaq.optimize()
on the provided objective function.
- property upper_bounds¶
Set the upper value bound for the optimization parameters.
- class cudaq.optimizers.COBYLA¶
- property initial_parameters¶
Set the initial parameter values for the optimization.
- property lower_bounds¶
Set the lower value bound for the optimization parameters.
- property max_iterations¶
Set the maximum number of optimizer iterations.
- optimize()¶
- optimize(self: cudaq.optimizers.COBYLA, dimensions: int, function: function) Tuple[float, List[float]]
Run
cudaq.optimize()
on the provided objective function.
- property upper_bounds¶
Set the upper value bound for the optimization parameters.
- class cudaq.optimizers.NelderMead¶
- property initial_parameters¶
Set the initial parameter values for the optimization.
- property lower_bounds¶
Set the lower value bound for the optimization parameters.
- property max_iterations¶
Set the maximum number of optimizer iterations.
- optimize()¶
- optimize(self: cudaq.optimizers.NelderMead, dimensions: int, function: function) Tuple[float, List[float]]
Run
cudaq.optimize()
on the provided objective function.
- property upper_bounds¶
Set the upper value bound for the optimization parameters.
- class cudaq.optimizers.LBFGS¶
- property initial_parameters¶
Set the initial parameter values for the optimization.
- property lower_bounds¶
Set the lower value bound for the optimization parameters.
- property max_iterations¶
Set the maximum number of optimizer iterations.
- optimize()¶
- optimize(self: cudaq.optimizers.LBFGS, dimensions: int, function: function) Tuple[float, List[float]]
Run
cudaq.optimize()
on the provided objective function.
- property upper_bounds¶
Set the upper value bound for the optimization parameters.
Gradients¶
- class cudaq.gradients.gradient¶
- class cudaq.gradients.CentralDifference¶
- compute()¶
- compute(self: cudaq.gradients.gradient, parameter_vector: List[float], function: function, funcAtX: float) List[float]
Compute the gradient of the provided
parameter_vector
with respect to its loss function, using theCentralDifference
method.
Noisy Simulation¶
- class cudaq.NoiseModel¶
The
NoiseModel
defines a set ofKrausChannel
’s applied to specific qubits after the invocation of specified quantum operations.- __init__()¶
- __init__(self: cudaq.NoiseModel) None
Construct an empty noise model.
- add_channel()¶
- add_channel(self: cudaq.NoiseModel, operator: str, qubits: List[int], channel: cudaq.KrausChannel) None
Add the given
KrausChannel
to be applied after invocation of the specified quantum operation.- Parameters:
operator (str) – The quantum operator to apply the noise channel to.
qubits (List[int]) – The qubit/s to apply the noise channel to.
channel (cudaq.KrausChannel) – The
KrausChannel
to apply to the specifiedoperator
on the specifiedqubits
.
- get_channels()¶
- get_channels(self: cudaq.NoiseModel, operator: str, qubits: List[int]) List[cudaq.KrausChannel]
Return the
KrausChannel
’s that make up this noise model.
- class cudaq.BitFlipChannel¶
Models the decoherence of the qubit state. Its constructor expects a float value,
probability
, representing the probability that the qubit flips from the 1-state to the 0-state, or vice versa. E.g, the probability of a random X-180 rotation being applied to the qubit. The probability of the qubit remaining in the same state is therefore1 - probability
.- __init__()¶
- __init__(self: cudaq.BitFlipChannel, probability: float) None
Initialize the
BitFlipChannel
with the providedprobability
.
- class cudaq.PhaseFlipChannel¶
Models the decoherence of the qubit phase. Its constructor expects a float value,
probability
, representing the probability of a random Z-180 rotation being applied to the qubit. The probability of the qubit phase remaining untouched is therefore1 - probability
.- __init__()¶
- __init__(self: cudaq.PhaseFlipChannel, probability: float) None
Initialize the
PhaseFlipChannel
with the providedprobability
.
- class cudaq.DepolarizationChannel¶
Models the decoherence of the qubit state and phase into a mixture of the computational basis states,
|0>
and|1>
. Its constructor expects a float value,probability
, representing the probability that this decay will occur. The qubit will remain untouched, therefore, with a probability of1 - probability
.- __init__()¶
- __init__(self: cudaq.DepolarizationChannel, probability: float) None
Initialize the
DepolarizationChannel
with the providedprobability
.
- class cudaq.AmplitudeDampingChannel¶
Models the dissipation of energy due to system interactions with the environment. Its constructor expects a float value,
probability
, representing the probablity that the qubit will decay to its ground state. The probability of the qubit remaining in the same state is therefore1 - probability
.- __init__()¶
- __init__(self: cudaq.AmplitudeDampingChannel, probability: float) None
Initialize the
AmplitudeDampingChannel
with the providedprobability
.
- class cudaq.KrausChannel¶
The
KrausChannel
is composed of a list ofKrausOperator
’s and is applied to a specific qubit or set of qubits.- __getitem__()¶
- __getitem__(self: cudaq.KrausChannel, index: int) cudaq.KrausOperator
Return the
KrausOperator
at the given index in thisKrausChannel
.
- append()¶
- append(self: cudaq.KrausChannel, arg0: cudaq.KrausOperator) None
Add a
KrausOperator
to thisKrausChannel
.
- class cudaq.KrausOperator¶
The
KrausOperator
is represented by a matrix and serves as an element of a quantum channel such thatSum Ki Ki^dag = I.
- property col_count¶
The number of columns in the matrix representation of this
KrausOperator
.
- property row_count¶
The number of rows in the matrix representation of this
KrausOperator
.
MPI Submodule¶
- cudaq.mpi.all_gather(*args, **kwargs)¶
Overloaded function.
Gather and scatter the
local
list of floating-point numbers, returning a concatenation of all lists across all ranks. The total global list size must be provided.Gather and scatter the
local
list of integers, returning a concatenation of all lists across all ranks. The total global list size must be provided.
- cudaq.mpi.broadcast()¶
-
Broadcast an array from a process (rootRank) to all other processes. The size of broadcast array must be provided.