From 482233b6b2328cae6e2d48b488da3aab9ea711f6 Mon Sep 17 00:00:00 2001 From: Thien Nguyen Date: Thu, 23 Jul 2020 09:10:01 -0400 Subject: [PATCH] Added QLanczos to the Python example Signed-off-by: Thien Nguyen --- python/examples/qite_example.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/python/examples/qite_example.py b/python/examples/qite_example.py index b5a75c20..22afce51 100644 --- a/python/examples/qite_example.py +++ b/python/examples/qite_example.py @@ -1,4 +1,4 @@ -# Simple 1-qubit demonstration of the Quatum Imaginary Time Evolution algorithm +# Simple 1-qubit demonstration of the Quatum Imaginary Time Evolution/QLanczos algorithm # Reference: https://www.nature.com/articles/s41567-019-0704-4 # Target H = 1/sqrt(2)(X + Z) # Expected minimum value: -1.0 @@ -12,9 +12,6 @@ qpu = xacc.getAccelerator('qpp') # Construct the Hamiltonian as an XACC PauliOperator ham = xacc.getObservable('pauli', '0.70710678118 X0 + 0.70710678118 Z0') -# We just need 1 qubit -buffer = xacc.qalloc(1) - # See Fig. 2 (e) of https://www.nature.com/articles/s41567-019-0704-4 # Horizontal axis: 0 -> 2.5 # The number of Trotter steps @@ -30,13 +27,27 @@ qite = xacc.getAlgorithm('qite', { 'step-size': stepSize, 'steps': nbSteps }) +# We just need 1 qubit +qiteBuffer = xacc.qalloc(1) +qiteResult = qite.execute(qiteBuffer) + +# Create the QLanczos algorithm +qLanczos = xacc.getAlgorithm('QLanczos', { + 'accelerator': qpu, + 'observable': ham, + 'step-size': stepSize, + 'steps': nbSteps + }) +qLanczosBuffer = xacc.qalloc(1) +qLanczosResult = qLanczos.execute(qLanczosBuffer) -result = qite.execute(buffer) +# Plot the energies +qiteEnergies = qiteBuffer.getInformation('exp-vals') +qLanczosEnergies = qLanczosBuffer.getInformation('exp-vals') -# Expected result: ~ -1 -print('Min energy value = ', buffer.getInformation('opt-val')) -E = buffer.getInformation('exp-vals') # Reproduce Fig. 2(e) of https://www.nature.com/articles/s41567-019-0704-4 -plt.plot(np.arange(0, nbSteps + 1) * stepSize, E, 'ro', label = 'XACC QITE') +plt.plot(np.arange(0, nbSteps + 1) * stepSize, qiteEnergies, 'ro-', label = 'QITE') +plt.plot(np.arange(0, (nbSteps + 1)//2) * 2 * stepSize, qLanczosEnergies, 'k*-', label = 'QLanczos') +plt.legend() plt.grid() plt.show() \ No newline at end of file -- GitLab