Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for validating trotterization order #1120

Merged
merged 4 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ Changelog
- Broadened the scope of `flake8` compliance to the include the `examples` and
`docs` directories, and thus the whole repository (@tommy-moffat, gh-1113).
- `DEFGATE ... AS PAULI-SUM` is now supported (@ecpeterson, gh-1125).
- Add unit test for validating Trotterization order (@jmbr, gh-1120).

### Bugfixes

- Minor fixes for examples/1.3_vqe_demo.py and examples/quantum_walk.ipynb
- Minor fixes for `examples/1.3_vqe_demo.py` and `examples/quantum_walk.ipynb`
(@appleby, gh-1116).

[v2.14](https://github.com/rigetti/pyquil/compare/v2.13.0...v2.14.0) (November 25, 2019)
Expand Down
2 changes: 1 addition & 1 deletion pyquil/paulis.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ def suzuki_trotter(trotter_order: int, trotter_steps: int) -> List[Tuple[float,
to the A operator, o=1 corresponds to the B operator, and w is the
coefficient in the exponential. For example, a second order Suzuki-Trotter
approximation to exp(A + B) results in the following
[(0.5/trotter_steps, 0), (1/trotteri_steps, 1),
[(0.5/trotter_steps, 0), (1/trotter_steps, 1),
(0.5/trotter_steps, 0)] * trotter_steps.

:param int trotter_order: order of Suzuki-Trotter approximation
Expand Down
34 changes: 34 additions & 0 deletions pyquil/tests/test_paulis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ID, UnequalLengthWarning, exponentiate, trotterize, is_zero,
check_commutation, commuting_sets, term_with_coeff, sI, sX, sY, sZ,
ZERO, is_identity)
from pyquil.unitary_tools import program_unitary
from pyquil.quil import Program


Expand Down Expand Up @@ -434,6 +435,39 @@ def test_trotterize():
assert prog == result_prog


def test_trotterize_order():
def expmi(hermitian_matrix):
"""Compute the matrix exponential of -1j * hermitian_matrix."""
L, Q = np.linalg.eigh(hermitian_matrix)
return Q @ np.diag(np.exp(-1j * L)) @ Q.conj().T

def error(order, time_step_length):
a_pauli = time_step_length * sZ(0) * sY(1) * sX(2)
a_program = a_pauli.program

b_pauli = time_step_length * sX(0) * sZ(1) * sY(2)
b_program = b_pauli.program

num_qubits = len(a_program.get_qubits())
assert num_qubits == len(b_program.get_qubits())

a = program_unitary(a_program, num_qubits)
b = program_unitary(b_program, num_qubits)
a_plus_b = a + b
exp_a_plus_b = expmi(time_step_length * a_plus_b)

trotter_program = trotterize(a_pauli, b_pauli, trotter_order=order)
trotter = program_unitary(trotter_program, num_qubits)

return np.linalg.norm(exp_a_plus_b - trotter, np.inf)

xs = 10**np.logspace(-1, -6, 10)
for order in [1, 2, 3, 4]:
ys = [error(order, float(x)) for x in xs]
p = np.polyfit(np.log10(xs), np.log10(ys), 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

assert p[0] >= order, f'Bound not satisfied with order={order}: the slope is {p[0]}'


def test_is_zero():
with pytest.raises(TypeError):
is_zero(1)
Expand Down