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

New propagation library created #99

Merged
merged 15 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion c3/libraries/propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def tf_expm(A, terms):
return r


def tf_expm_dynamic(A, acc=1e-4):
def tf_expm_dynamic(A, acc=1e-5):
"""
Matrix exponential by the series method with specified accuracy.

Expand Down
1 change: 0 additions & 1 deletion c3/utils/tf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ def Id_like(A):
@tf.function
def tf_kron(A, B):
"""Kronecker product of 2 matrices."""
# TODO make kronecker product general to different dimensions
dims = tf.shape(A) * tf.shape(B)
tensordot = tf.tensordot(A, B, axes=0)
reshaped = tf.reshape(tf.transpose(tensordot, perm=[0, 2, 1, 3]), dims)
Expand Down
41 changes: 41 additions & 0 deletions test/test_exp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python
# coding: utf-8

# In[3]:


import numpy as np
import tensorflow as tf
import pytest

from numpy.testing import assert_array_almost_equal as almost_equal
from c3.libraries.constants import Id, X, Y, Z
from c3.libraries.propagation import tf_expm, tf_expm_dynamic

theta = 2 * np.pi * np.random.rand(1)


@pytest.mark.unit
def test_exponentiation_1() -> None:
"""Testing that exponentiation methods are almost equal in the numpy sense.
Check that, given P = a*X+b*Y+c*Z with a, b, c random normalized numbers,
exp(i theta P) = cos(theta)*Id + sin(theta)*P"""
a = np.random.rand(1)
b = np.random.rand(1)
c = np.random.rand(1)
norm = np.sqrt(a ** 2 + b ** 2 + c ** 2)
# Normalized random coefficients
a = a / norm
b = b / norm
c = c / norm

P = a * X + b * Y + c * Z
theta = 2 * np.pi * np.random.rand(1)
rot = theta * P
res = np.cos(theta) * Id + 1j * np.sin(theta) * P

terms = 25

almost_equal(tf_expm(1j * rot, terms), res)
lazyoracle marked this conversation as resolved.
Show resolved Hide resolved
almost_equal(tf_expm_dynamic(1j * rot), res)
lazyoracle marked this conversation as resolved.
Show resolved Hide resolved
almost_equal(tf.linalg.expm(1j * rot), res)