optimizer_utils — Foundation Utilities¶
Shared utilities used by all optimizers in QuantumExpectedValueFunctionProject/.
Provides VariableRegister (qubit-encoded integer with operator circuits) and
PowerSystem_1Bus (problem specification container).
VariableRegister¶
QuantumExpectedValueFunctionProject.optimizer_utils.VariableRegister ¶
class VariableRegister: Use this object to store an integer variable in a register of qubits, and to perform interactions between registers of qubits. We use a register of qubits 's', which represent binary variables 'y' to encode the integer variable 'v'. Depending on the encoding, this integer variable 'v' is created by a linear combination of binary variables 'sum b*y', and 'b' depends on the type of encoding.
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
Functions¶
__unaryLessThanOperator ¶
__unaryLessThanOperator If this > other, return e^i(this - other). Otherwise, Identity
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__binaryLessThanOperator ¶
__binaryLessThanOperator If this > other, return e^i(this - other). Otherwise, Identity
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__binaryLessThanValue ¶
__binaryLessThanValue
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__binaryNumberOperator ¶
__binaryNumberOperator Return amplitude * v in Little Endian
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__unaryNumberOperator ¶
__unaryNumberOperator Return amplitude * v encoded as a unary operator
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__unarySwapOperator ¶
__unarySwapOperator Return integer encoding as a unary operator For now, the swap interactions are all-to-all, single Trotter step
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__binarySwapOperator ¶
__binarySwapOperator Return integer swap operation for the binary encoding
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__unaryProductOperator ¶
__unaryProductOperator
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__binaryProductOperator ¶
__binaryProductOperator
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__binarySquaredOperator ¶
__binarySquaredOperator
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
__unarySquaredOperator ¶
__unarySquaredOperator
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
swapOperator ¶
swapOperator Return a circuit which does an amplitude * "integer swap" between two registers.
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
numberOperator ¶
numberOperator Return a circuit which does amplitude*v using the corresponding encoding.
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
productOperator ¶
productOperator Return a circuit which, given two registers, computes amplitudethisother
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
squaredOperator ¶
squaredOperator Return a circuit which does amplitudethisthis
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
lessThanOperator ¶
lessThanOperator Return a circuit which does phase amplitude*(this-other) if this > other, otherwise 1
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
oracleLessThanOperator ¶
Return a circuit which rotates an ancilla by (this-other) if this > other
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
lessThanValue ¶
lessThanValue Return a circuit which does phase amplitude*(this-value) if this > value, otherwise Identity
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
getValue ¶
Given a register's bitstring, return the integer it encodes
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
setValue ¶
Given an integer, return a circuit which creates that integer
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
PowerSystem_1Bus¶
QuantumExpectedValueFunctionProject.optimizer_utils.PowerSystem_1Bus ¶
PowerSystem_1Bus(gas_costs, wind_costs, decision_levels, undersatisfied_cost, demand, pdf, normalization=None)
All-to-all lossless single-bus power system specification.
Stores all parameters of the two-stage stochastic unit commitment problem and provides utilities for normalizing costs, evaluating the classical solution, and post-processing quantum measurement results.
The single-bus approximation ignores transmission losses, so every generator contributes directly to meeting total demand \(d\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gas_costs
|
List of per-unit costs for gas generators. |
required | |
wind_costs
|
List of per-unit operational costs for wind turbines. |
required | |
decision_levels
|
Number of discrete levels per variable (integer range 0 to |
required | |
undersatisfied_cost
|
Penalty cost per unit of unmet demand (recourse cost \(c_r\)). |
required | |
demand
|
Total demand \(d\) that must be met. |
required | |
pdf
|
Dict mapping scenario tuples \((\xi_0, \xi_1, \ldots)\) to probabilities. |
required | |
normalization
|
Optional |
None
|
Note
Discretization constraint: cost * (decision_levels - 1) == max_cost.
Initialize the power system with costs, discretization, and PDF.
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
Attributes¶
scenarios
instance-attribute
¶
self.sample_list = [] self.sample_hist = {} denominators = [Fraction(Decimal(str(self.pdf[scene]))).denominator for scene in self.scenarios] # iterate over 'scenarios' to preserve order of vars
get least common multiple of the Pr's denominators¶
lcm = 1 for denom in denominators: lcm = lcm*denom//gcd(lcm,denom)
convert Pr to frequency of occurency in SAA¶
for scene, prob in self.pdf.items(): reps = prob * lcm self.sample_hist[scene] = int(reps) for _ in range(int(np.around(reps))): self.sample_list.append(scene)
Functions¶
getFirstStageCosts ¶
Given a list of decisions over the gas variables, return the cost and wind decision variables for each
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
cobylaSolve ¶
Use cobyla to solve the optimization problem. Either solve the entire two-stage problem via direct expansion or, given a gas decision, solve only the second stage
Source code in QuantumExpectedValueFunctionProject/optimizer_utils.py
plotMeasurementsVExpectedCost ¶
plotMeasurementsVExpectedCost Given a dictionary of gas generator measured results, plot the measurement count vs. expected cost of that decision It is the quantum optimizer's job to create a histogram of gas decisions.