def __init__( self, circuits: Iterable[QuantumCircuit] | QuantumCircuit | None = None, observables: Iterable[SparsePauliOp] | SparsePauliOp | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, ): """ Creating an instance of an Estimator, or using one in a ``with`` context opens a session that holds resources until the instance is ``close()`` ed or the context is exited. Args: circuits: Quantum circuits that represent quantum states. observables: Observables. parameters: Parameters of quantum circuits, specifying the order in which values will be bound. Defaults to ``[circ.parameters for circ in circuits]`` The indexing is such that ``parameters[i, j]`` is the j-th formal parameter of ``circuits[i]``. Raises: QiskitError: For mismatch of circuits and parameters list. """ if circuits is not None or observables is not None or parameters is not None: warn( "The BaseEstimator 'circuits', `observables`, `parameters` kwarg are deprecated " "as of 0.22.0 and will be removed no earlier than 3 months after the " "release date. You can use 'run' method to append objects.", DeprecationWarning, 2, ) if isinstance(circuits, QuantumCircuit): circuits = (circuits, ) self._circuits = [] if circuits is None else list(circuits) if isinstance(observables, SparsePauliOp): observables = (observables, ) self._observables = [] if observables is None else list(observables) # To guarantee that they exist as instance variable. # With only dynamic set, the python will not know if the attribute exists or not. self._circuit_ids: dict[int, int] = self._circuit_ids self._observable_ids: dict[int, int] = self._observable_ids if parameters is None: self._parameters = [circ.parameters for circ in self._circuits] else: self._parameters = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): raise QiskitError( f"Different number of parameters ({len(self._parameters)}) and " f"circuits ({len(self._circuits)})") for i, (circ, params) in enumerate(zip(self._circuits, self._parameters)): if circ.num_parameters != len(params): raise QiskitError( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." )
def __init__( self, circuits: Iterable[QuantumCircuit], observables: Iterable[SparsePauliOp], parameters: Iterable[Iterable[Parameter]] | None = None, ): """ Creating an instance of an Estimator, or using one in a ``with`` context opens a session that holds resources until the instance is ``close()`` ed or the context is exited. Args: circuits: quantum circuits that represent quantum states observables: observables parameters: parameters of quantum circuits, specifying the order in which values will be bound. Defaults to ``[circ.parameters for circ in circuits]`` The indexing is such that ``parameters[i, j]`` is the j-th formal parameter of ``circuits[i]``. Raises: QiskitError: for mismatch of circuits and parameters list. """ self._circuits = tuple(circuits) self._observables = tuple(observables) if parameters is None: self._parameters = tuple(circ.parameters for circ in self._circuits) else: self._parameters = tuple(ParameterView(par) for par in parameters) if len(self._parameters) != len(self._circuits): raise QiskitError( f"Different number of parameters ({len(self._parameters)} and " f"circuits ({len(self._circuits)}") for i, (circ, params) in enumerate(zip(self._circuits, self._parameters)): if circ.num_parameters != len(params): raise QiskitError( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." )
def __init__( self, circuits: Iterable[QuantumCircuit] | QuantumCircuit | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, ): """ Args: circuits: Quantum circuits to be executed. parameters: Parameters of each of the quantum circuits. Defaults to ``[circ.parameters for circ in circuits]``. Raises: QiskitError: For mismatch of circuits and parameters list. """ if circuits is not None or parameters is not None: warn( "The BaseSampler 'circuits', and `parameters` kwarg are deprecated " "as of 0.22.0 and will be removed no earlier than 3 months after the " "release date. You can use 'run' method to append objects.", DeprecationWarning, 2, ) if isinstance(circuits, QuantumCircuit): circuits = (circuits,) self._circuits = [] if circuits is None else list(circuits) # To guarantee that they exist as instance variable. # With only dynamic set, the python will not know if the attribute exists or not. self._circuit_ids: dict[int, int] = self._circuit_ids if parameters is None: self._parameters = [circ.parameters for circ in self._circuits] else: self._parameters = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): raise QiskitError( f"Different number of parameters ({len(self._parameters)}) " f"and circuits ({len(self._circuits)})" )
def __init__( self, circuits: Iterable[QuantumCircuit], parameters: Iterable[Iterable[Parameter]] | None = None, ): """ Args: circuits: quantum circuits to be executed parameters: parameters of quantum circuits Defaults to ``[circ.parameters for circ in circuits]`` Raises: QiskitError: for mismatch of circuits and parameters list. """ self._circuits = tuple(circuits) if parameters is None: self._parameters = tuple(circ.parameters for circ in self._circuits) else: self._parameters = tuple(ParameterView(par) for par in parameters) if len(self._parameters) != len(self._circuits): raise QiskitError( f"Different number of parameters ({len(self._parameters)} " f"and circuits ({len(self._circuits)}")
def run( self, circuits: Sequence[QuantumCircuit], parameter_values: Sequence[Sequence[float]] | None = None, parameters: Sequence[Sequence[Parameter]] | None = None, **run_options, ) -> Job: """Run the job of the sampling of bitstrings. Args: circuits: the list of circuit objects. parameter_values: Parameters to be bound to the circuit. parameters: Parameters of each of the quantum circuits. Defaults to ``[circ.parameters for circ in circuits]``. run_options: Backend runtime options used for circuit execution. Returns: The job object of the result of the sampler. The i-th result corresponds to ``circuits[i]`` evaluated with parameters bound as ``parameter_values[i]``. Raises: QiskitError: Invalid arguments are given. """ # Support ndarray if isinstance(parameter_values, np.ndarray): parameter_values = parameter_values.tolist() # Allow optional if parameter_values is None: for i, circuit in enumerate(circuits): if circuit.num_parameters != 0: raise QiskitError( f"The {i}-th circuit ({len(circuits)}) is parameterised," "but parameter values are not given." ) parameter_values = [[]] * len(circuits) if parameters is None: parameter_views = [circ.parameters for circ in circuits] else: parameter_views = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): raise QiskitError( f"Different number of parameters ({len(self._parameters)}) and " f"circuits ({len(self._circuits)})" ) for i, (circ, params) in enumerate(zip(self._circuits, self._parameters)): if circ.num_parameters != len(params): raise QiskitError( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." ) # Validation if len(circuits) != len(parameter_values): raise QiskitError( f"The number of circuits ({len(circuits)}) does not match " f"the number of parameter value sets ({len(parameter_values)})." ) for i, (circuit, parameter_value) in enumerate(zip(circuits, parameter_values)): if len(parameter_value) != circuit.num_parameters: raise QiskitError( f"The number of values ({len(parameter_value)}) does not match " f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit." ) return self._run(circuits, parameter_values, parameter_views, **run_options)
def run( self, circuits: Sequence[QuantumCircuit], observables: Sequence[BaseOperator | PauliSumOp], parameter_values: Sequence[Sequence[float]] | None = None, parameters: Sequence[Sequence[Parameter]] | None = None, **run_options, ) -> Job: """Run the job of the estimation of expectation value(s). ``circuits``, ``observables``, and ``parameter_values`` should have the same length. The i-th element of the result is the expectation of observable .. code-block:: python obs = observables[i] for the state prepared by .. code-block:: python circ = circuits[i] with bound parameters .. code-block:: python values = parameter_values[i]. Args: circuits: the list of circuit objects. observables: the list of observable objects. parameter_values: concrete parameters to be bound. parameters: Parameters of quantum circuits, specifying the order in which values will be bound. Defaults to ``[circ.parameters for circ in circuits]`` The indexing is such that ``parameters[i, j]`` is the j-th formal parameter of ``circuits[i]``. run_options: runtime options used for circuit execution. Returns: The job object of EstimatorResult. Raises: QiskitError: Invalid arguments are given. """ # Support ndarray if isinstance(parameter_values, np.ndarray): parameter_values = parameter_values.tolist() # Allow optional if parameter_values is None: for i, circuit in enumerate(circuits): if circuit.num_parameters != 0: raise QiskitError(f"The {i}-th circuit is parameterised," "but parameter values are not given.") parameter_values = [[]] * len(circuits) if parameters is None: parameter_views = [circ.parameters for circ in circuits] else: parameter_views = [ParameterView(par) for par in parameters] if len(self._parameters) != len(self._circuits): raise QiskitError( f"Different number of parameters ({len(self._parameters)}) and " f"circuits ({len(self._circuits)})") for i, (circ, params) in enumerate(zip(self._circuits, self._parameters)): if circ.num_parameters != len(params): raise QiskitError( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." ) # Validation if len(circuits) != len(observables): raise QiskitError( f"The number of circuits ({len(circuits)}) does not match " f"the number of observables ({len(observables)}).") if len(circuits) != len(parameter_values): raise QiskitError( f"The number of circuits ({len(circuits)}) does not match " f"the number of parameter value sets ({len(parameter_values)})." ) for i, (circuit, parameter_value) in enumerate(zip(circuits, parameter_values)): if len(parameter_value) != circuit.num_parameters: raise QiskitError( f"The number of values ({len(parameter_value)}) does not match " f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit." ) for i, (circuit, observable) in enumerate(zip(circuits, observables)): if circuit.num_qubits != observable.num_qubits: raise QiskitError( f"The number of qubits of the {i}-th circuit ({circuit.num_qubits}) does " f"not match the number of qubits of the {i}-th observable " f"({observable.num_qubits}).") return self._run(circuits, observables, parameter_values, parameter_views, **run_options)