quast_decisiontree.algorithms.hybrid.variational
quast_decisiontree.algorithms.hybrid.variational
VariationalResult
dataclass
Result of a variational algorithm execution.
Attributes:
| Name | Type | Description |
|---|---|---|
counts |
dict[str, int]
|
Final measurement counts from the optimized circuit. |
optimal_params |
ndarray | None
|
Optimized parameter array. |
cost_history |
list[float]
|
List of cost values recorded during optimization. |
num_evals |
int
|
Total number of cost function evaluations. |
Source code in src/quast_decisiontree/algorithms/hybrid/variational.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
counts
instance-attribute
counts
optimal_params
class-attribute
instance-attribute
optimal_params = None
cost_history
class-attribute
instance-attribute
cost_history = field(default_factory=list)
num_evals
class-attribute
instance-attribute
num_evals = 0
__init__
__init__(
counts,
optimal_params=None,
cost_history=list(),
num_evals=0,
)
VariationalAlgorithm
Bases: HybridAlgorithm
Template for custom variational quantum-classical optimization loops.
Implements the standard variational pattern
- Initialize parameters for the ansatz
- Variational loop (driven by optimizer): ansatz(qv, params) → backend.run() → cl_cost_function(counts) → scalar cost
- Final measurement with optimized parameters → return VariationalResult
The optimizer (built by OptimizerBuilder) drives the loop via its .minimize() method. The template builds a cost closure that the optimizer calls repeatedly.
Hyperparameters (set by upstream nodes via problem_data):
- ansatz: Callable(QuantumVariable, np.ndarray) → None.
Applies parameterized gates to a QuantumVariable.
Must expose a num_params attribute (int) indicating the parameter count.
- optimizer: Optimizer instance (built by OptimizerBuilder).
Must have a .minimize(fun, x0, bounds=None) method returning a result with .x.
- shots: Number of measurement shots per circuit evaluation.
- init_params: Optional initial parameter array for warm-starting.
If None, random initialization in [0, 2π) is used.
Input keys (problem-specific data): - cl_cost_function: Callable(Dict[str, int]) → float. Maps measurement counts to a scalar cost value. - num_qubits: int. Number of qubits for the problem.
Source code in src/quast_decisiontree/algorithms/hybrid/variational.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
HYPERPARAMS
class-attribute
HYPERPARAMS = [
HyperParam(
name="ansatz",
hparam_type=None,
description="Parameterized ansatz: callable(qv, params) with .num_params attribute",
),
HyperParam(
name="optimizer",
hparam_type=None,
description="Optimizer instance with .minimize(fun, x0, bounds=None) → result with .x. Built by OptimizerBuilder.",
),
HyperParam(
name="shots",
hparam_type=int,
description="Number of shots per circuit evaluation",
default=1024,
test=lambda x: x > 0,
),
HyperParam(
name="init_params",
hparam_type=None,
description="Optional initial parameter array for warm-starting (None = random)",
default=None,
),
]
INPUT_KEYS
class-attribute
INPUT_KEYS = ('cl_cost_function', 'num_qubits')
reset
reset()
Clear input, backend, and algorithm state after execution.
Source code in src/quast_decisiontree/algorithms/hybrid/variational.py
99 100 101 102 | |
run_algorithm
run_algorithm(backend, input)
Execute the variational optimization loop.
Returns:
| Type | Description |
|---|---|
VariationalResult
|
VariationalResult with counts, optimal parameters, and cost history. |
Source code in src/quast_decisiontree/algorithms/hybrid/variational.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |