Skip to content

VQEBuilder

Source code in q3as/algo/vqe.py
class VQEBuilder:
    _app: Optional[Application] = None
    _ansatz: Optional[Union[QuantumCircuit, AnsatzCallback]] = EfficientSU2()
    _observables: Optional[ObservablesArrayLike] = None
    _initial_params: Optional[ArrayLike] = None
    _optimizer: Optimizer = COBYLA()
    _maxiter: int = 1000

    def app(self, app: Application):
        """
        Set the `Application` to use for interpretation of results
        """
        self._app = app
        return self

    def ansatz(
        self,
        qc: Union[QuantumCircuit, AnsatzCallback],
    ) -> VQEBuilder:
        """
        Set the ansatz to use for the VQE.
        This can either be a QuantumCircuit or a callback that takes the observables as argument and returns a QuantumCircuit.
        This defaults to the EfficientSU2 ansatz.
        """
        self._ansatz = qc
        return self

    def observables(self, obs: ObservablesArrayLike) -> VQEBuilder:
        """
        Set the observables to estimate. E.g. the Hamiltonian of the system.
        If no observables are given, the Hamiltonian of the application is used
        """
        self._observables = obs
        return self

    def initial_params(self, params: ArrayLike) -> VQEBuilder:
        """
        Set the initial parameters of the ansatz. If no parameters are given, the initial parameters are set to 0.
        """
        self._initial_params = params
        return self

    def optimizer(self, opt: Optimizer) -> VQEBuilder:
        """
        Set the optimizer to use for the VQE. This defaults to COBYLA.
        """
        self._optimizer = opt
        return self

    def maxiter(self, maxiter: int) -> VQEBuilder:
        """
        Set the maximum number of iterations for the VQE. This defaults to 1000.
        """
        self._maxiter = maxiter
        return self

    def build(self) -> VQE:
        """
        Build the VQE instance
        """
        if self._observables is None:
            if self._app is not None:
                observables = self._app.hamiltonian()
            else:
                raise ValueError("Observables are required")
        else:
            observables = self._observables
        observables = ObservablesArray.coerce(observables)

        if self._ansatz is None:
            raise ValueError("Ansatz is required")
        if isinstance(self._ansatz, QuantumCircuit):
            ansatz = self._ansatz
        else:
            ansatz = self._ansatz(observables)

        if self._initial_params is not None:
            _initial_params = np.array(self._initial_params)
            if len(_initial_params) != ansatz.num_parameters:
                raise ValueError("Initial parameters must match ansatz")
        else:
            _initial_params = np.zeros(ansatz.num_parameters)

        return VQE(
            app=self._app,
            ansatz=ansatz,
            observables=observables,
            initial_params=_initial_params,
            optimizer=self._optimizer,
            maxiter=self._maxiter,
        )

    def run(
        self,
        estimator: Estimator,
        sampler: Optional[Sampler] = None,
        callback: Optional[Callable[[VQEIteration], None]] = None,
    ) -> VQEResult:
        """
        Run the VQE
        """
        return self.build().run(estimator, sampler, callback)

    def send(self, api: Client, estimator: RunOptions = RunOptions()) -> Job:
        """
        Send the VQE job to the API
        """
        return self.build().send(api, estimator)

ansatz(qc)

Set the ansatz to use for the VQE. This can either be a QuantumCircuit or a callback that takes the observables as argument and returns a QuantumCircuit. This defaults to the EfficientSU2 ansatz.

Source code in q3as/algo/vqe.py
def ansatz(
    self,
    qc: Union[QuantumCircuit, AnsatzCallback],
) -> VQEBuilder:
    """
    Set the ansatz to use for the VQE.
    This can either be a QuantumCircuit or a callback that takes the observables as argument and returns a QuantumCircuit.
    This defaults to the EfficientSU2 ansatz.
    """
    self._ansatz = qc
    return self

app(app)

Set the Application to use for interpretation of results

Source code in q3as/algo/vqe.py
def app(self, app: Application):
    """
    Set the `Application` to use for interpretation of results
    """
    self._app = app
    return self

build()

Build the VQE instance

Source code in q3as/algo/vqe.py
def build(self) -> VQE:
    """
    Build the VQE instance
    """
    if self._observables is None:
        if self._app is not None:
            observables = self._app.hamiltonian()
        else:
            raise ValueError("Observables are required")
    else:
        observables = self._observables
    observables = ObservablesArray.coerce(observables)

    if self._ansatz is None:
        raise ValueError("Ansatz is required")
    if isinstance(self._ansatz, QuantumCircuit):
        ansatz = self._ansatz
    else:
        ansatz = self._ansatz(observables)

    if self._initial_params is not None:
        _initial_params = np.array(self._initial_params)
        if len(_initial_params) != ansatz.num_parameters:
            raise ValueError("Initial parameters must match ansatz")
    else:
        _initial_params = np.zeros(ansatz.num_parameters)

    return VQE(
        app=self._app,
        ansatz=ansatz,
        observables=observables,
        initial_params=_initial_params,
        optimizer=self._optimizer,
        maxiter=self._maxiter,
    )

initial_params(params)

Set the initial parameters of the ansatz. If no parameters are given, the initial parameters are set to 0.

Source code in q3as/algo/vqe.py
def initial_params(self, params: ArrayLike) -> VQEBuilder:
    """
    Set the initial parameters of the ansatz. If no parameters are given, the initial parameters are set to 0.
    """
    self._initial_params = params
    return self

maxiter(maxiter)

Set the maximum number of iterations for the VQE. This defaults to 1000.

Source code in q3as/algo/vqe.py
def maxiter(self, maxiter: int) -> VQEBuilder:
    """
    Set the maximum number of iterations for the VQE. This defaults to 1000.
    """
    self._maxiter = maxiter
    return self

observables(obs)

Set the observables to estimate. E.g. the Hamiltonian of the system. If no observables are given, the Hamiltonian of the application is used

Source code in q3as/algo/vqe.py
def observables(self, obs: ObservablesArrayLike) -> VQEBuilder:
    """
    Set the observables to estimate. E.g. the Hamiltonian of the system.
    If no observables are given, the Hamiltonian of the application is used
    """
    self._observables = obs
    return self

optimizer(opt)

Set the optimizer to use for the VQE. This defaults to COBYLA.

Source code in q3as/algo/vqe.py
def optimizer(self, opt: Optimizer) -> VQEBuilder:
    """
    Set the optimizer to use for the VQE. This defaults to COBYLA.
    """
    self._optimizer = opt
    return self

run(estimator, sampler=None, callback=None)

Run the VQE

Source code in q3as/algo/vqe.py
def run(
    self,
    estimator: Estimator,
    sampler: Optional[Sampler] = None,
    callback: Optional[Callable[[VQEIteration], None]] = None,
) -> VQEResult:
    """
    Run the VQE
    """
    return self.build().run(estimator, sampler, callback)

send(api, estimator=RunOptions())

Send the VQE job to the API

Source code in q3as/algo/vqe.py
def send(self, api: Client, estimator: RunOptions = RunOptions()) -> Job:
    """
    Send the VQE job to the API
    """
    return self.build().send(api, estimator)