Skip to content

Commit

Permalink
Feedback, updated comments, constants, etc...
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Vasilevsky committed Jan 14, 2025
1 parent 40b611c commit 01a669b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
50 changes: 25 additions & 25 deletions samples/algorithms/SimpleVQE.qs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
/// This is an example of a Variational Quantum Eigensolver (VQE).
/// This example includes:
/// 1. Simple classical optimization to find minimum of a multi-variable function
/// in order to find the approximation to the minimum eigenvalue of a hamiltonian
/// in order to find an approximation to the minimum eigenvalue of a hamiltonian
/// 2. Finding Hamiltonian expectation value as a weighted sum of terms.
/// 3. Finding one term expectation value by performing multiple shots.
/// 4. Ansatz state preparation similar to the circuit in the referenced paper.
/// To keep this sample simple hamiltonian terms are randomly selected.
/// To keep this sample simple hamiltonian terms are generated randomly.
///
/// # Reference
/// Ground-state energy estimation of the water molecule on a trapped ion quantum
Expand All @@ -29,32 +29,37 @@ operation Main() : Double {
// Find the approximation to the minimum eigenvalue of a Hamiltonian
// by varying ansatz parameters to minimize its expectation value.
SimpleDescent(
// Use 1000 shots when estimating hamiltonian terms
// Use a number of shots when estimating hamiltonian terms
// Actual VQE implementations may require very large number of shots.
FindHamiltonianExpectationValue(_, 100),
// Start from these angles for ansatz state preparation
[1.0, 1.0],
// Use initial step pi/8 to find minimum
PI() / 8.0,
// Initial step to search for minimum
0.5,
// Stop optimization if step is 0
0.0,
// Stop optimization after 100 attempts to improve
100
// Stop optimization after several attempts.
// Actual VQE would need to make enough iterations
// to find energy with sufficient chemical accuracy.
50
)
}

/// # Summary
/// Find expectation value of a Hamiltonian given parameters for the
/// ansatz state and number of shots to evaluate each term.
/// Different VQE applications will have different measurements and
/// coefficients depending on the Hamiltonian being evaluated.
operation FindHamiltonianExpectationValue(thetas : Double[], shots : Int) : Double {
let terms = [
([PauliZ, PauliI, PauliI, PauliI], 0.16),
([PauliI, PauliI, PauliZ, PauliI], 0.25),
([PauliI, PauliI, PauliZ, PauliI], -0.25),
([PauliZ, PauliZ, PauliI, PauliI], 0.17),
([PauliI, PauliI, PauliZ, PauliZ], 0.45),
([PauliX, PauliX, PauliX, PauliX], 0.2),
([PauliY, PauliY, PauliY, PauliY], 0.1),
([PauliY, PauliX, PauliX, PauliY], 0.02),
([PauliX, PauliY, PauliY, PauliX], 0.22),
([PauliY, PauliX, PauliX, PauliY], -0.02),
([PauliX, PauliY, PauliY, PauliX], -0.22),
];
mutable value = 0.0;
for (basis, coefficient) in terms {
Expand Down Expand Up @@ -86,20 +91,23 @@ operation FindTermExpectationValue(

/// # Summary
/// Prepare the ansatz state for given parameters on a qubit register
/// This is an example of ansatz state preparation similar to the
/// unitary couple clustered method used in the referenced paper.
/// Actual VQE application will have different ansatz preparation operations.
operation PrepareAnsatzState(qs : Qubit[], thetas : Double[]) : Unit {
BosonicExitationTerm(thetas[0], qs[0], qs[2]);
CNOT(qs[0], qs[1]);
NonBosonicExitataionTerm(thetas[1], qs[0], qs[1], qs[2], qs[3]);
}

/// # Summary
/// Bosonic exitation circuit
/// Bosonic exitation circuit from the referenced paper.
operation BosonicExitationTerm(
theta : Double,
moX : Qubit,
moY : Qubit
) : Unit {

X(moX);
Adjoint S(moX);
Rxx(theta, moX, moY);
S(moX);
Expand All @@ -109,38 +117,30 @@ operation BosonicExitationTerm(
}

/// # Summary
/// Non-bosonic exitation circuit
/// Non-bosonic exitation circuit from the referenced paper.
operation NonBosonicExitataionTerm(
theta : Double,
moXsoX : Qubit,
moXsoY : Qubit,
moYsoX : Qubit,
moYsoY : Qubit
) : Unit {

Adjoint S(moXsoX);
within {
CNOT(moXsoX, moYsoY);
CNOT(moXsoX, moYsoX);
CNOT(moXsoX, moXsoY);
H(moXsoX);
} apply {
Rz(theta, moXsoX);
CNOT(moXsoY, moXsoX);
Rz(theta, moXsoX);
CNOT(moYsoY, moXsoX);
Rz(-theta, moXsoX);
CNOT(moXsoY, moXsoX);
Rz(-theta, moXsoX);
} apply {
Adjoint S(moYsoX);
CNOT(moYsoX, moXsoX);
Rx(theta, moXsoX);
CNOT(moXsoY, moXsoX);
Rx(theta, moXsoX);
CNOT(moYsoY, moXsoX);
Rz(-theta, moXsoX);
CNOT(moXsoY, moXsoX);
Rz(-theta, moXsoX);
}
S(moYsoX);
}
Expand All @@ -149,6 +149,7 @@ operation NonBosonicExitataionTerm(
/// Simple classical optimizer. A descent to a local minimum of function `f`.
/// Tries to takes steps in all directions and proceeds if the new point is better.
/// If no moves result in function value improvement the step size is halved.
/// Actual VQE implementations use more elaborate optimizers.
operation SimpleDescent(
f : Double[] => Double,
initialPoint : Double[],
Expand All @@ -171,17 +172,16 @@ operation SimpleDescent(
mutable hadImprovement = false;
for i in IndexRange(initialPoint) {
let nextPoint = bestPoint w/ i <- bestPoint[i] + currentStep;
let nextValue = f(nextPoint);
let nextValue = f(nextPoint); // Evaluate quantum part
currentAttempt = currentAttempt + 1;
if nextValue < bestValue {
hadImprovement = true;
bestValue = nextValue;
bestPoint = nextPoint;
Message($"Value improved to {bestValue}.");
}

let nextPoint = bestPoint w/ i <- bestPoint[i] - currentStep;
let nextValue = f(nextPoint);
let nextValue = f(nextPoint); // Evaluate quantum part
currentAttempt = currentAttempt + 1;
if nextValue < bestValue {
hadImprovement = true;
Expand Down
32 changes: 14 additions & 18 deletions samples_test/src/tests/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,25 +259,21 @@ pub const SIMPLEISING_EXPECT: Expect =
pub const SIMPLEISING_EXPECT_DEBUG: Expect =
expect!["[Zero, Zero, Zero, One, One, Zero, One, One, Zero]"];
pub const SIMPLEVQE_EXPECT: Expect = expect![[r#"
Beginning descent from value 0.9795.
Value improved to 0.9511000000000001.
Value improved to 0.8876000000000001.
Value improved to 0.8764000000000001.
Value improved to 0.8702000000000001.
Value improved to 0.8641000000000002.
Value improved to 0.8549000000000001.
Descent done. Attempts: 100, Step: 0.00000037450702829239286, Arguments: [1.5886651273511148, 1.3926990816987241], Value: 0.8549000000000001.
0.8549000000000001"#]];
Beginning descent from value 0.43300000000000005.
Value improved to 0.35300000000000004.
Value improved to 0.3454.
Value improved to 0.3422.
Value improved to 0.3216.
Descent done. Attempts: 52, Step: 0.0009765625, Arguments: [1.5, 1.0625], Value: 0.3216.
0.3216"#]];
pub const SIMPLEVQE_EXPECT_DEBUG: Expect = expect![[r#"
Beginning descent from value 0.9795.
Value improved to 0.9511000000000001.
Value improved to 0.8876000000000001.
Value improved to 0.8764000000000001.
Value improved to 0.8702000000000001.
Value improved to 0.8641000000000002.
Value improved to 0.8549000000000001.
Descent done. Attempts: 100, Step: 0.00000037450702829239286, Arguments: [1.5886651273511148, 1.3926990816987241], Value: 0.8549000000000001.
0.8549000000000001"#]];
Beginning descent from value 0.43300000000000005.
Value improved to 0.35300000000000004.
Value improved to 0.3454.
Value improved to 0.3422.
Value improved to 0.3216.
Descent done. Attempts: 52, Step: 0.0009765625, Arguments: [1.5, 1.0625], Value: 0.3216.
0.3216"#]];
pub const SUPERDENSECODING_EXPECT: Expect = expect!["((false, true), (false, true))"];
pub const SUPERDENSECODING_EXPECT_DEBUG: Expect = expect!["((false, true), (false, true))"];
pub const SUPERPOSITION_EXPECT: Expect = expect!["Zero"];
Expand Down

0 comments on commit 01a669b

Please sign in to comment.