qpandalite.pytorch package#

Submodules#

qpandalite.pytorch.batch_executor module#

Batch execution utilities for quantum circuits.

Provides parallel execution of multiple circuits using ThreadPoolExecutor or multiprocessing for performance optimization.

qpandalite.pytorch.batch_executor.batch_execute(circuits, executor, n_workers=4)[源代码]

Execute multiple circuits in parallel.

参数:
  • circuits (list[Circuit]) -- List of circuits to execute

  • executor (Callable[[Circuit], np.ndarray]) -- Function that executes a single circuit and returns results

  • n_workers (int) -- Number of parallel workers (threads)

返回:

List of results from each circuit execution

返回类型:

list[np.ndarray]

示例

>>> def simulate(c):
...     sim = OriginIR_Simulator()
...     return sim.simulate(c.originir)
>>> results = batch_execute([c1, c2, c3], simulate, n_workers=4)

qpandalite.pytorch.gradient module#

Parameter-shift rule gradient computation for quantum circuits.

The parameter-shift rule allows exact gradient computation for parametric quantum gates without finite differences. For gates with generator G:

∂⟨G(θ)⟩/∂θ = (⟨G(θ + π/2)⟩ - ⟨G(θ - π/2)⟩) / 2

This module provides functions to compute gradients for all parameters in a parametric circuit.

qpandalite.pytorch.gradient.compute_all_gradients(circuit, expectation_fn, shift=1.5707963267948966)[源代码]

Compute gradients for all parameters in a circuit.

Uses the parameter-shift rule for each parameter independently.

参数:
  • circuit (Circuit) -- Parametric circuit

  • expectation_fn (Callable[[Circuit], float]) -- Function that computes expectation value from a circuit

  • shift (float) -- Shift value for parameter-shift rule

返回:

Dictionary mapping parameter names to gradient values

返回类型:

dict[str, float]

示例

>>> grads = compute_all_gradients(circuit, expectation)
>>> print(grads)  # {'theta': 0.5, 'phi': -0.3}
qpandalite.pytorch.gradient.parameter_shift_gradient(circuit, param_name, expectation_fn, shift=1.5707963267948966)[源代码]

Compute gradient using the parameter-shift rule.

For a parametric gate G(θ), the gradient of an expectation value is:

∂⟨G(θ)⟩/∂θ = 0.5 * (⟨G(θ + π/2)⟩ - ⟨G(θ - π/2)⟩)

参数:
  • circuit (Circuit) -- Parametric circuit with the parameter

  • param_name (str) -- Name of the parameter to differentiate

  • expectation_fn (Callable[[Circuit], float]) -- Function that computes expectation value from a circuit

  • shift (float) -- Shift value (default π/2 for standard Pauli rotation gates)

返回:

Gradient value

返回类型:

float

示例

>>> def expectation(c):
...     # Simulate and compute <Z0>
...     sim.simulate(c.originir)
...     return sim.expectation([("Z", [0])])
>>> grad = parameter_shift_gradient(circuit, "theta", expectation)

qpandalite.pytorch.quantum_layer module#

QuantumLayer: PyTorch nn.Module for quantum circuits.

This module provides a PyTorch-compatible layer that wraps a parametric quantum circuit, enabling gradient-based optimization via the parameter-shift rule.

class qpandalite.pytorch.quantum_layer.QuantumLayer(*args, **kwargs)[源代码]

基类:object

Placeholder when PyTorch is not installed.

Module contents#

PyTorch integration for quantum machine learning.

This module provides tools for integrating quantum circuits with PyTorch: - Parameter-shift rule gradient computation - QuantumLayer nn.Module for hybrid quantum-classical models - Batch execution utilities

class qpandalite.pytorch.QuantumLayer(*args, **kwargs)[源代码]

基类:object

Placeholder when PyTorch is not installed.

qpandalite.pytorch.batch_execute(circuits, executor, n_workers=4)[源代码]

Execute multiple circuits in parallel.

参数:
  • circuits (list[Circuit]) -- List of circuits to execute

  • executor (Callable[[Circuit], np.ndarray]) -- Function that executes a single circuit and returns results

  • n_workers (int) -- Number of parallel workers (threads)

返回:

List of results from each circuit execution

返回类型:

list[np.ndarray]

示例

>>> def simulate(c):
...     sim = OriginIR_Simulator()
...     return sim.simulate(c.originir)
>>> results = batch_execute([c1, c2, c3], simulate, n_workers=4)
qpandalite.pytorch.batch_execute_with_params(circuit_template, param_values, executor, n_workers=4)[源代码]

Execute a circuit template with different parameter bindings.

Creates multiple bound circuits from a template and executes them in parallel.

参数:
  • circuit_template (Circuit) -- Circuit template with symbolic parameters

  • param_values (list[dict[str, float]]) -- List of parameter value dictionaries to bind

  • executor (Callable[[Circuit], np.ndarray]) -- Function that executes a single circuit

  • n_workers (int) -- Number of parallel workers

返回:

List of results from each parameter binding

返回类型:

list[np.ndarray]

示例

>>> params = [{'theta': 0.1}, {'theta': 0.2}, {'theta': 0.3}]
>>> results = batch_execute_with_params(circuit, params, simulate)
qpandalite.pytorch.compute_all_gradients(circuit, expectation_fn, shift=1.5707963267948966)[源代码]

Compute gradients for all parameters in a circuit.

Uses the parameter-shift rule for each parameter independently.

参数:
  • circuit (Circuit) -- Parametric circuit

  • expectation_fn (Callable[[Circuit], float]) -- Function that computes expectation value from a circuit

  • shift (float) -- Shift value for parameter-shift rule

返回:

Dictionary mapping parameter names to gradient values

返回类型:

dict[str, float]

示例

>>> grads = compute_all_gradients(circuit, expectation)
>>> print(grads)  # {'theta': 0.5, 'phi': -0.3}
qpandalite.pytorch.parameter_shift_gradient(circuit, param_name, expectation_fn, shift=1.5707963267948966)[源代码]

Compute gradient using the parameter-shift rule.

For a parametric gate G(θ), the gradient of an expectation value is:

∂⟨G(θ)⟩/∂θ = 0.5 * (⟨G(θ + π/2)⟩ - ⟨G(θ - π/2)⟩)

参数:
  • circuit (Circuit) -- Parametric circuit with the parameter

  • param_name (str) -- Name of the parameter to differentiate

  • expectation_fn (Callable[[Circuit], float]) -- Function that computes expectation value from a circuit

  • shift (float) -- Shift value (default π/2 for standard Pauli rotation gates)

返回:

Gradient value

返回类型:

float

示例

>>> def expectation(c):
...     # Simulate and compute <Z0>
...     sim.simulate(c.originir)
...     return sim.expectation([("Z", [0])])
>>> grad = parameter_shift_gradient(circuit, "theta", expectation)