1. Metrics¶
graphiq.metrics ¶
Classes to compute metrics on a circuit and/or system states
CircuitCnotCount ¶
Bases: MetricBase
A metric which calculates the circuit's CNOT count
Source code in graphiq/metrics.py
__init__(log_steps=1, n_cnot_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
n_cnot_penalty | function | a function which calculates a "cost"/penalty as a function of circuit's number of CNOTs | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the number of emitter-emitter CNOT gates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from number of CNOTs. By default, this is the CNOT count itself |
Source code in graphiq/metrics.py
CircuitDepth ¶
Bases: MetricBase
A metric which calculates the circuit depth
Source code in graphiq/metrics.py
__init__(log_steps=1, depth_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
depth_penalty | function | a function which calculates a "cost"/penalty as a function of circuit depth | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the circuit depth
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from circuit depth. By default, this is the circuit depth itself |
Source code in graphiq/metrics.py
CircuitEmitterCount ¶
Bases: MetricBase
A metric which calculates the circuit's number of emitters'
Source code in graphiq/metrics.py
__init__(log_steps=1, n_emitter_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
n_emitter_penalty | function | a function which calculates a "cost"/penalty as a function of circuit's number of emitters | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the number of emitters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from number of emitters. By default, this is the emitter count itself |
Source code in graphiq/metrics.py
CircuitMaxEmitDepth ¶
Bases: MetricBase
A metric which calculates the circuit's maximum emitter depth
Source code in graphiq/metrics.py
__init__(log_steps=1, depth_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
depth_penalty | function | a function which calculates a "cost"/penalty as a function of circuit depth | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the circuit depth
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from circuit depth. By default, this is the circuit depth itself |
Source code in graphiq/metrics.py
CircuitMaxEmitEffDepth ¶
Bases: MetricBase
A metric which calculates the circuit's maximum emitter effective depth
Source code in graphiq/metrics.py
__init__(log_steps=1, depth_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
depth_penalty | function | a function which calculates a "cost"/penalty as a function of circuit depth | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the circuit depth
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from circuit depth. By default, this is the circuit depth itself |
Source code in graphiq/metrics.py
CircuitMaxEmitResetDepth ¶
Bases: MetricBase
A metric which calculates the circuit's maximum emitter reset depth
Source code in graphiq/metrics.py
__init__(log_steps=1, depth_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
depth_penalty | function | a function which calculates a "cost"/penalty as a function of circuit depth | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the circuit depth
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from circuit depth. By default, this is the circuit depth itself |
Source code in graphiq/metrics.py
CircuitMeasureCount ¶
Bases: MetricBase
A metric which calculates the circuit's number of measurements
Source code in graphiq/metrics.py
__init__(log_steps=1, m_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
m_penalty | function | a function which calculates a "cost"/penalty as a function of number of measurement | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the number of measurements
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from number of measurements. By default, this is the measurement count itself |
Source code in graphiq/metrics.py
CircuitUnitaryCount ¶
Bases: MetricBase
A metric which calculates the circuit depth
Source code in graphiq/metrics.py
__init__(log_steps=1, n_unitary_penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
n_unitary_penalty | function | a function which calculates a "cost"/penalty as a function of circuit's number of unitary gates | None |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Calculates a scalar function of the number of unitary gates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | state which was created by the circuit. This is not actually used by this metric object, but is nonetheless provided to guarantee a uniform API between Metric-type objects | required |
circuit | CircuitBase (or a subclass of it) | the circuit to evaluate | required |
Returns:
| Type | Description |
|---|---|
float | int | the scalar penalty resulting from number of unitaries. By default, this is the unitary count itself |
Source code in graphiq/metrics.py
GraphMetric ¶
Bases: MetricBase
A class to calculate a given graph metric
Source code in graphiq/metrics.py
__init__(graph, log_steps=1, penalty=None, *args, **kwargs) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
m_penalty | function | a function which calculates a "cost"/penalty as a function of number of measurement | required |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
evaluate(graph_metric) ¶
Calculates a metric for a given graph state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_metric | str | The list of valid metrics are "max_between", "max_close", "min_close", "mean_nei_deg", "max_deg", "node_connect", "edge_connect", "assort", "radius", "diameter", "periphery", "center", "cluster", "local_efficiency", "global_efficiency", "node", "avg_shortest_path", "n_edges", "pop" | required |
Returns:
| Type | Description |
|---|---|
float | int | the resulting value for the metric |
Source code in graphiq/metrics.py
Infidelity ¶
Bases: MetricBase
Source code in graphiq/metrics.py
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 | |
__init__(target, log_steps=1, *args, **kwargs) ¶
Creates an Infidelity Metric object, which computes 1-fidelity with respect to the ideal_state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target | QuantumState | the ideal state against which we compute fidelity | required |
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
Returns:
| Type | Description |
|---|---|
None | nothing |
Raises:
| Type | Description |
|---|---|
AssertionError | if targe is not a valid density matrix |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Evaluates the infidelity between a state, \(\rho\), and a target state, \(\rho_{t}\).
The infidelity is \(1- F(\rho, \rho_{t})\)
For density matrices the fidelity is: $$ F(\rho, \rho_{t}):=\text{Tr}[\sqrt{\sqrt{\rho} \rho_{t} \sqrt{\rho}}]^2 $$ or if either \(\rho\) or \(\rho_{t}\) is pure, then it simplifies to: $$ F(\rho, \rho_{t}):=\text{Tr}[\rho \rho_{t}] $$
Using the branched mixed stabilizer representation, the fidelity is: $$ F(\rho, T_t) := \sum_i p_i F(T_i, T_{t}) $$ which assumes the target state is pure and represented by a single tableau \(T_t\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | the state to evaluate | required |
circuit | CircuitBase (or subclass of it) | circuit which generated state Not used for the fidelity evaluation, but argument is provided for API consistency | required |
Returns:
| Type | Description |
|---|---|
float | infidelity = 1 - fidelity |
Raises:
| Type | Description |
|---|---|
AssertionError | if the state is not a valid density matrix |
Source code in graphiq/metrics.py
MetricBase ¶
Bases: ABC
Base class for a metric.
Metrics should be scalar values computed on the circuit and/or system states If a metric is used as a cost function, we aim to minimize it (i.e. smaller metric means better performance); this is not, however, required of metrics in general.
Source code in graphiq/metrics.py
__init__(log_steps=1, *args, **kwargs) ¶
Create a MetricBase object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
Returns:
| Type | Description |
|---|---|
None | the function returns nothing |
Source code in graphiq/metrics.py
increment() ¶
Counts up the number of times a given metric has been evaluated
Returns:
| Type | Description |
|---|---|
None | this function returns nothing |
Metrics ¶
Bases: MetricBase
Wraps around one or more metric functions, evaluating each and logging the values
Source code in graphiq/metrics.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
per_metric_log property ¶
The joint log of all metric functions
Returns:
| Type | Description |
|---|---|
dict (keys are metric class names, values are the logs) | the log itself |
__init__(metrics_list, metric_weight=None, log_steps=1, *args, **kwargs) ¶
Create a Metrics object which acts as a wrapper around Metric functions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics_list | list | metrics to evaluate | required |
metric_weight | None, numpy.ndarray, list, | Function | some representation of how to weigh the different metric results against one another if None, all metrics provided are weighted equally (by 1) if a list or ndarray, the metrics are a linear combination weighted by the list/ndarray values Otherwise, metric_weight is a function, that can make any mathematical function of the individual metric values. | None |
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
Returns:
| Type | Description |
|---|---|
None | function returns nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Evaluate each metric function contained by the Metrics object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | the state on which to evaluate the metrics | required |
circuit | CircuitBase (or a subclass of it) | the circuit on which to evaluate the metrics | required |
Returns:
| Type | Description |
|---|---|
None | this function returns nothing |
Source code in graphiq/metrics.py
TraceDistance ¶
Bases: MetricBase
Source code in graphiq/metrics.py
__init__(target, log_steps=1, *args, **kwargs) ¶
Creates a TraceDistance Metric object, which computes the trace distance between the current state and the target state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target | QuantumState | the ideal state against which we compute fidelity | required |
log_steps | int | the metric values are computed at every log_steps optimization step | 1 |
Returns:
| Type | Description |
|---|---|
None | nothing |
Source code in graphiq/metrics.py
evaluate(state, circuit) ¶
Evaluates the trace distance between the state to the target state.
The trace distance is computed between two density matrices \(\rho\) and \(\sigma\) as: $$ T(\rho, \sigma) = \frac{1}{2} \text{Tr}\left( \sqrt{ (\rho - \sigma)^2 } \right) = \frac{1}{2} \sum_i | \lambda_i | $$
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state | QuantumState | the state to evaluate | required |
circuit | CircuitBase (or subclass of it) | circuit which generated state Not used for the trace distance evaluation, but argument is provided for API consistency | required |
Returns:
| Type | Description |
|---|---|
float | the trace distance |
Source code in graphiq/metrics.py
graph_met_value(graph_metric, g) ¶
Evaluates the graph metric for the given graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_metric | str | the abbreviation for the graph metric to be evaluated | required |
g | nx.Graph | graph at study | required |
Returns:
| Type | Description |
|---|---|
int | float | the graph metric value |