paddle_quantum.loss.measure
The source file of the class for the measurement.
- class paddle_quantum.loss.measure.ExpecVal(hamiltonian, shots=0)
Bases:
OperatorThe class of the loss function to compute the expectation value for the observable.
This interface can make you using the expectation value for the observable as the loss function.
- Parameters:
hamiltonian (Hamiltonian) – The input observable.
shots (int | None) – The number of measurement shots. Defaults to
0. Now it just need to be input when the backend is QuLeaf.
- forward(state, decompose=False)
Compute the expectation value of the observable with respect to the input state.
The value computed by this function can be used as a loss function to optimize.
- Parameters:
state (State) – The input state which will be used to compute the expectation value.
decompose (bool | None) – Defaults to
False. If decompose isTrue, it will return the expectation value of each term.
- Raises:
NotImplementedError – The backend is wrong or not supported.
- Returns:
The expectation value. If the backend is QuLeaf, it is computed by sampling.
- Return type:
Tensor | List[Tensor]
- class paddle_quantum.loss.measure.Measure(measure_basis='z')
Bases:
OperatorCompute the probability of the specified measurement result.
- Parameters:
measure_basis (Iterable[Tensor] | str | None) – Specify the basis of the measurement. Defaults to
'z'.- Raises:
NotImplementedError – Currently we just support the z basis.
- forward(state, qubits_idx='full', desired_result=None)
Compute the probability of measurement to the input state.
- Parameters:
state (State) – The quantum state to be measured.
qubits_idx (Iterable[int] | int | str | None) – The index of the qubits to be measured. Defaults to
'full'which means measure all the qubits.desired_result (Iterable[str] | str | None) – Specify the results of the measurement to return. Defaults to
Nonewhich means return the probability of all the results.
- Raises:
NotImplementedError – The backend is wrong or not supported.
NotImplementedError – The qubit index is wrong or not supported.
NotImplementedError – Currently we just support the z basis.
- Returns:
The probability of the measurement.
- Return type:
Tensor