quantrs2_sim/advanced_variational_algorithms/
isingcostfunction_traits.rs1use crate::circuit_interfaces::{InterfaceCircuit, InterfaceGate, InterfaceGateType};
12use crate::error::{Result, SimulatorError};
13use scirs2_core::random::prelude::*;
14
15use super::functions::CostFunction;
16use super::types::IsingCostFunction;
17
18impl CostFunction for IsingCostFunction {
19 fn evaluate(&self, _parameters: &[f64], circuit: &InterfaceCircuit) -> Result<f64> {
20 let num_qubits = circuit.num_qubits;
21 let mut cost = 0.0;
22 for term in &self.problem_hamiltonian.terms {
23 match term.pauli_string.as_str() {
24 "ZZ" if term.qubits.len() == 2 => {
25 cost += term.coefficient.re;
26 }
27 "Z" if term.qubits.len() == 1 => {
28 cost += term.coefficient.re;
29 }
30 _ => {}
31 }
32 }
33 Ok(cost)
34 }
35 fn get_observables(&self) -> Vec<String> {
36 self.problem_hamiltonian
37 .terms
38 .iter()
39 .map(|term| term.pauli_string.clone())
40 .collect()
41 }
42 fn is_variational(&self) -> bool {
43 true
44 }
45}