This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 923
/
HydrogenSimulation.qs
107 lines (81 loc) · 4.96 KB
/
HydrogenSimulation.qs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Chemistry.Samples.Hydrogen {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Chemistry.JordanWigner;
open Microsoft.Quantum.Simulation;
open Microsoft.Quantum.Characterization;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
// We now use the Q# component of the chemistry library to obtain
// quantum operations that implement real-time evolution by
// the chemistry Hamiltonian. Below, we consider two examples.
// - Trotter simulation algorithm
// - Qubitization simulation algorithm
// These operations are invoked as oracles in the quantum phase estimation
// algorithm to extract energy estimates of various eigenstate of the
// Hamiltonian.
// The returned energy estimate is chosen probabilistically, depending on
// the overlap of the initial trial state. By default, we greedily
// fill spin-orbitals to minimize the diagonal component of the one-electron
// energies.
//////////////////////////////////////////////////////////////////////////
// Using Trotterization //////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// We can now use Canon's phase estimation algorithms to
// learn the ground state energy using the above simulation.
operation GetEnergyByTrotterization (qSharpData : JordanWignerEncodingData, nBitsPrecision : Int, trotterStepSize : Double, trotterOrder : Int) : (Double, Double) {
// The data describing the Hamiltonian for all these steps is contained in
// `qSharpData`
let (nSpinOrbitals, fermionTermData, statePrepData, energyOffset) = qSharpData!;
// We use a Product formula, also known as `Trotterization` to
// simulate the Hamiltonian.
let (nQubits, (rescaleFactor, oracle)) = TrotterStepOracle(qSharpData, trotterStepSize, trotterOrder);
// The operation that creates the trial state is defined below.
// By default, greedy filling of spin-orbitals is used.
let statePrep = PrepareTrialState(statePrepData, _);
// We use the Robust Phase Estimation algorithm
// of Kimmel, Low and Yoder.
let phaseEstAlgorithm = RobustPhaseEstimation(nBitsPrecision, _, _);
// This runs the quantum algorithm and returns a phase estimate.
let estPhase = EstimateEnergy(nQubits, statePrep, oracle, phaseEstAlgorithm);
// We obtain the energy estimate by rescaling the phase estimate
// with the trotterStepSize. We also add the constant energy offset
// to the estimated energy.
let estEnergy = estPhase * rescaleFactor + energyOffset;
// We return both the estimated phase, and the estimated energy.
return (estPhase, estEnergy);
}
//////////////////////////////////////////////////////////////////////////
// Using Qubitization ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// The following is identical to the approach above using Trotterization,
// except that we replace the oracle with a quantum walk created by the
// qubitization procedure. This results in a more accurate simulation,
// but at the cost of larger qubit overhead.
operation GetEnergyByQubitization (qSharpData : JordanWignerEncodingData, nBitsPrecision : Int) : (Double, Double) {
// The data describing the Hamiltonian for all these steps is contained in
// `qSharpData`
let (nSpinOrbitals, fermionTermData, statePrepData, energyOffset) = qSharpData!;
// The parameters required by Qubitization is contained in this
// convenience function.
let (nQubits, (oneNorm, oracle)) = QubitizationOracle(qSharpData);
// The operation that creates the trial state is defined below.
// By default, greedy filling of spin-orbitals is used.
let statePrep = PrepareTrialState(statePrepData, _);
// We use the Robust Phase Estimation algorithm
// of Kimmel, Low and Yoder.
let phaseEstAlgorithm = RobustPhaseEstimation(nBitsPrecision, _, _);
// This runs the quantum algorithm and returns a phase estimate.
let estPhase = EstimateEnergy(nQubits, statePrep, oracle, phaseEstAlgorithm);
// Note that the quantum walk applies e^{isin^{-1}{H/oneNorm}}, in contrast to
// real-time evolution e^{iHt} by a Product formula.
// Thus We obtain the energy estimate by applying Sin(.) to the phase estimate
// then rescaling by the coefficient one-norm of the Hamiltonian.
// We also add the constant energy offset to the estimated energy.
let estEnergy = Sin(estPhase) * oneNorm + energyOffset;
// We return both the estimated phase, and the estimated energy.
return (estPhase, estEnergy);
}
}