Use Qrunch public types

Import types listed in the qrunch.type_defs export index rather than from their internal definition modules. These are the names declared in qrunch.type_defs.__all__. The stability policy covers those names for annotations and the documented methods of supported extension protocols. The modules that define these types and the constructors of implementation classes are not part of that promise.

  • Implementation classes, such as calculators and intermediate creators: use them in annotations. Get instances from a public creator or as results from a public method, rather than calling their constructors.

  • Structural protocols used as extension points, such as ReferenceCircuitProvider: implement the documented methods and pass your object to the API. You do not need to import or subclass the protocol.

  • Options dataclasses: construct these directly from qrunch.options and pass them to creators. They are not exported by qrunch.type_defs.

Importing the qrunch.type_defs module does not load every exported type. Importing a specific name resolves that type and may load optional dependencies. If you only need a name for static type checking, use postponed annotations and put the import under if TYPE_CHECKING.

Examples of where this is useful:

Example 1: Annotate a builder helper

The return annotation names the calculator type, but the creator constructs it. The options, in contrast, are constructed directly:

from __future__ import annotations

from typing import TYPE_CHECKING

import qrunch as qc

if TYPE_CHECKING:
    from qrunch.type_defs import ConfigurationInteractionLowestStatesCalculator


def make_ci_calculator() -> ConfigurationInteractionLowestStatesCalculator:
    return (
        qc.lowest_states_calculator_creator()
        .configuration_interaction()
        .standard()
        .with_options(qc.options.ConfigurationInteractionOptions())
        .create()
    )

Example 2: Implement a custom reference circuit provider

SqDRIFT accepts objects with the two methods documented by ReferenceCircuitProvider. Suppose reference_circuit prepares a reference state in the fermionic qubit mapping for problem, for example a circuit from a fermionic VQE result on that same problem. This provider returns a fresh copy each time. It is specific to that problem and does not support the paired-electron approximation.

import qrunch as qc


class SavedReference:
    def __init__(self, circuit):
        self._circuit = circuit

    def supports_paired_approximation(self) -> bool:
        return False

    def create_circuits(self, ground_state_problem, *, number_of_states, paired_electron_approximation) -> tuple[Circuit, ...]:
        return (self._circuit.copy(),)


calculator = (
    qc.lowest_states_calculator_creator()
    .quantum_selected_configuration_interaction()
    .sqdrift()
    .choose_reference_circuit_providers()
    .custom([SavedReference(reference_circuit)])
    .create()
)
result = calculator.calculate(problem)

See Calculate Energies With PRIME-SqDRIFT and QSCI for more on selecting reference states.