Skip to content

Commit ffb98da

Browse files
idk3babbush
authored andcommitted
Function for bounding number of Trotter steps required (#121)
* Added function for computing Trotter step requirements * Change + test to enforce rtype int in Python2 * Corrected Trotter number bound
1 parent 89d5cc4 commit ffb98da

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/fermilib/utils/_trotter_error.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""Module to compute the second order Trotter error."""
1414
from future.utils import iteritems
1515

16-
from math import sqrt
16+
from math import sqrt, ceil
1717
from scipy.linalg import expm
1818

1919
from fermilib.config import *
@@ -164,3 +164,23 @@ def error_bound(terms, tight=False):
164164
error += 4.0 * abs(coefficient_a) * error_a ** 2
165165

166166
return error
167+
168+
169+
def trotter_steps_required(trotter_error_bound, time, energy_precision):
170+
"""Determine the number of Trotter steps for accurate simulation.
171+
172+
Args:
173+
trotter_error_bound (float): Upper bound on Trotter error in the
174+
state of interest.
175+
time (float): The total simulation time.
176+
energy_precision (float): Acceptable shift in state energy.
177+
178+
Returns:
179+
The integer minimum number of Trotter steps required for
180+
simulation to the desired precision.
181+
182+
Notes:
183+
The number of Trotter steps required is an upper bound on the
184+
true requirement, which may be lower.
185+
"""
186+
return int(ceil(time * sqrt(trotter_error_bound / energy_precision)))

src/fermilib/utils/_trotter_error_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,16 @@ def test_error_bound_qubit_tight_less_than_loose_integration(self):
148148
terms = [QubitOperator('X1'), QubitOperator('Y1'), QubitOperator('Z1')]
149149
self.assertLess(error_bound(terms, tight=True),
150150
error_bound(terms, tight=False))
151+
152+
153+
class TrotterStepsRequiredTest(unittest.TestCase):
154+
def test_trotter_steps_required(self):
155+
self.assertEqual(trotter_steps_required(
156+
trotter_error_bound=0.3, time=2.5, energy_precision=0.04), 7)
157+
158+
def test_trotter_steps_required_negative_time(self):
159+
self.assertEqual(trotter_steps_required(
160+
trotter_error_bound=0.1, time=3.3, energy_precision=0.11), 4)
161+
162+
def test_return_type(self):
163+
self.assertIsInstance(trotter_steps_required(0.1, 0.1, 0.1), int)

0 commit comments

Comments
 (0)