MatrixFunctional
class MatrixFunctional(main_diag, off_diag)
Bases: qiskit.algorithms.linear_solvers.observables.linear_system_observable.LinearSystemObservable
A class for the matrix functional of the vector solution to the linear systems.
Examples
import numpy as np
from qiskit import QuantumCircuit
from qiskit.algorithms.linear_solvers.observables.matrix_functional import MatrixFunctional
from qiskit.transpiler.passes import RemoveResetInZeroState
from qiskit.opflow import StateFn
tpass = RemoveResetInZeroState()
vector = [1.0, -2.1, 3.2, -4.3]
observable = MatrixFunctional(1, -1 / 3)
init_state = vector / np.linalg.norm(vector)
num_qubits = int(np.log2(len(vector)))
# Get observable circuits
obs_circuits = observable.observable_circuit(num_qubits)
qcs = []
for obs_circ in obs_circuits:
qc = QuantumCircuit(num_qubits)
qc.isometry(init_state, list(range(num_qubits)), None)
qc.append(obs_circ, list(range(num_qubits)))
qcs.append(tpass(qc.decompose()))
# Get observables
observable_ops = observable.observable(num_qubits)
state_vecs = []
# First is the norm
state_vecs.append((~StateFn(observable_ops[0]) @ StateFn(qcs[0])).eval())
for i in range(1, len(observable_ops), 2):
state_vecs += [(~StateFn(observable_ops[i]) @ StateFn(qcs[i])).eval(),
(~StateFn(observable_ops[i + 1]) @ StateFn(qcs[i + 1])).eval()]
# Obtain result
result = observable.post_processing(state_vecs, num_qubits)
# Obtain analytical evaluation
exact = observable.evaluate_classically(init_state)
Parameters
- main_diag (
float
) – The main diagonal of the tridiagonal Toeplitz symmetric matrix to compute the functional. - off_diag (
int
) – The off diagonal of the tridiagonal Toeplitz symmetric matrix to compute the functional.
Methods
evaluate_classically
MatrixFunctional.evaluate_classically(solution)
Evaluates the given observable on the solution to the linear system.
Parameters
solution (Union
[array
, QuantumCircuit
]) – The solution to the system as a numpy array or the circuit that prepares it.
Return type
float
Returns
The value of the observable.
observable
MatrixFunctional.observable(num_qubits)
The observable operators.
Parameters
num_qubits (int
) – The number of qubits on which the observable will be applied.
Return type
Union
[TensoredOp
, List
[TensoredOp
]]
Returns
The observable as a list of sums of Pauli strings.
observable_circuit
MatrixFunctional.observable_circuit(num_qubits)
The circuits to implement the matrix functional observable.
Parameters
num_qubits (int
) – The number of qubits on which the observable will be applied.
Return type
Union
[QuantumCircuit
, List
[QuantumCircuit
]]
Returns
The observable as a list of QuantumCircuits.
post_processing
MatrixFunctional.post_processing(solution, num_qubits, scaling=1)
Evaluates the matrix functional on the solution to the linear system.
Parameters
- solution (
Union
[float
,List
[float
]]) – The list of probabilities calculated from the circuit and the observable. - num_qubits (
int
) – The number of qubits where the observable was applied. - scaling (
float
) – Scaling of the solution.
Return type
float
Returns
The value of the absolute average.
Raises
ValueError – If the input is not in the correct format.