Last active
April 29, 2018 05:43
-
-
Save goerz/403855f70e5161eceb43af086a0e1088 to your computer and use it in GitHub Desktop.
Testingthe general algebraic structures in QNET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sympy import symbols | |
from qnet.algebra import * | |
def check(checks): | |
for (func, args) in checks: | |
try: | |
print("%s: %s" % (func.__name__, func(*args))) | |
except Exception as exc_info: | |
print("%s: Exception %r" % (func.__name__, exc_info)) | |
# Vector Space properties | |
def has_addition(a, b): | |
return (a + b is not None) | |
def has_scalar_product(a): | |
return (symbols('alpha') * a is not None) | |
def has_add_associativity(a, b, c): | |
return a + (b + c) == (a + b) + c | |
def has_add_commutativity(a, b): | |
return a + b == b + a | |
def has_add_neutral_element(a, zero): | |
return a + zero == a | |
def has_add_inverse(a, zero): | |
return a + (-a) == zero | |
def has_scalar_mul_compatibility(a): | |
alpha, beta = symbols('alpha, beta') | |
return alpha * (beta * a) == (alpha * beta) * a | |
def has_scalar_identity(a): | |
return 1 * a == a | |
def has_distributivity(a, b): | |
alpha, beta = symbols('alpha, beta') | |
return ( | |
((alpha * (a + b)).expand() == alpha * a + alpha * b) and | |
(((alpha + beta) * a).expand() == alpha * a + beta * a)) | |
# Hilbert space properties | |
def has_inner_product(a, b, inner, is_scalar): | |
return is_scalar(inner(a, b)) | |
def check_hilbertspace_properties(a, b, c, zero, inner, is_scalar): | |
print( | |
"Checking Hilbert space properties for a=%s, b=%s, c=%s, " | |
"zero=%s, inner=%s, is_scalar=%s" | |
% (a, b, c, zero, inner.__name__, is_scalar.__name__)) | |
checks = [ | |
# Vector space | |
[has_addition, [a, b]], | |
[has_scalar_product, [a]], | |
[has_add_associativity, [a, b, c]], | |
[has_add_commutativity, [a, b]], | |
[has_add_neutral_element, [a, zero]], | |
[has_add_inverse, [a, zero]], | |
[has_scalar_mul_compatibility, [a]], | |
[has_scalar_identity, [a]], | |
[has_distributivity, [a, b]], | |
# Hilbert space | |
[has_inner_product, [a, b, inner, is_scalar]], | |
] | |
check(checks) | |
# General Algebra properties | |
def has_product(a, b): | |
return (a * b is not None) | |
def has_left_distributive_law(a, b, c): | |
return (a * (b + c)).expand() == a * b + a * c | |
def has_right_distributive_law(a, b, c): | |
return ((a + b) * c).expand() == a * c + b * c | |
def has_product_associativity(a, b, c): | |
return (a * b) * c == a * (b * c) | |
def has_product_neutral_element(a, one): | |
return a * one == a | |
# C* Algebra properties | |
def has_involution(a, adjoint): | |
return adjoint(adjoint(a)) == a | |
def has_adjoint_distribute_over_sum(a, b, adjoint): | |
return adjoint(a + b).expand() == adjoint(a) + adjoint(b) | |
def has_adjoint_distribute_over_sum(a, b, adjoint): | |
return adjoint(a * b) == adjoint(b) * adjoint(a) | |
def has_scalar_adjoint(a, adjoint): | |
alpha = symbols('alpha') | |
return adjoint(alpha * a) == alpha.conjugate() * adjoint(a) | |
def has_cstar_identity(a, adjoint, inner): | |
norm = lambda x: inner(x, x) # technically, sqrt, but it shouldn't matter | |
return norm(adjoint(a) * a) == norm(a) * norm(a) | |
def check_cstar_properties(a, b, c, zero, one, inner, adjoint, is_scalar): | |
print( | |
"Checking C* Algebra properties for a=%s, b=%s, c=%s, " | |
"zero=%s, one=%s, inner=%s, adjoint=%s, is_scalar=%s" | |
% (a, b, c, zero, one, inner.__name__, adjoint.__name__, | |
is_scalar.__name__)) | |
checks = [ | |
# General Algebra | |
[has_product, [a, b]], | |
[has_left_distributive_law, [a, b, c]], | |
[has_right_distributive_law, [a, b, c]], | |
[has_product_associativity, [a, b, c]], | |
[has_product_neutral_element, [a, one]], | |
# C* algebra | |
[has_involution, [a, adjoint]], | |
[has_adjoint_distribute_over_sum, [a, b, adjoint]], | |
[has_scalar_adjoint, [a, adjoint]], | |
[has_cstar_identity, [a, adjoint, inner]], | |
] | |
check(checks) | |
check_hilbertspace_properties(a, b, c, zero, inner, is_scalar) | |
############################################################################### | |
print("\n\n*** Check for Kets on the same space ***") | |
check_cstar_properties( | |
LocalKet('a', hs=1), LocalKet('b', hs=1), LocalKet('c', hs=1), ZeroKet, | |
TrivialKet, | |
inner=lambda a, b: a.adjoint() * b, | |
adjoint=lambda a: a.adjoint(), | |
is_scalar=lambda x: isinstance(x, BraKet)) | |
# Note: this *should* be `is_scalar=lambda x: isinstance(x, Scalar)`, but | |
# `Scalar` isn't properly implemented yet, see | |
# https://github.com/mabuchilab/QNET/issues/68 | |
print("\n\n*** Check for Kets on disjunct spaces ***") | |
check_cstar_properties( | |
LocalKet('a', hs=1), LocalKet('b', hs=2), LocalKet('c', hs=3), ZeroKet, | |
TrivialKet, | |
inner=lambda a, b: a.adjoint() * b, | |
adjoint=lambda a: a.adjoint(), | |
is_scalar=lambda x: isinstance(x, BraKet)) | |
print("\n\n*** Check for Operators on same space ***") | |
check_cstar_properties( | |
OperatorSymbol('A', hs=1), OperatorSymbol('B', hs=1), | |
OperatorSymbol('C', hs=1), ZeroOperator, IdentityOperator, | |
inner=lambda a, b: OperatorTrace( | |
a.adjoint() * b, over_space=(a.space * b.space)), | |
adjoint=lambda a: a.adjoint(), | |
is_scalar=lambda x: isinstance(x, OperatorTrace)) | |
# Note: See above; we'll have to distinguish between FullOperatorTrace (Scalar) | |
# and PartialOperatorTrace at some point, but it's not implemented yet | |
print("\n\n*** Check for Operators on disjunct spaces ***") | |
check_cstar_properties( | |
OperatorSymbol('A', hs=1), OperatorSymbol('B', hs=2), | |
OperatorSymbol('C', hs=3), ZeroOperator, IdentityOperator, | |
inner=lambda a, b: OperatorTrace( | |
a.adjoint() * b, over_space=(a.space * b.space)), | |
adjoint=lambda a: a.adjoint(), | |
is_scalar=lambda x: isinstance(x, OperatorTrace)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*** Check for Kets on the same space *** | |
Checking C* Algebra properties for a=|a⟩⁽¹⁾, b=|b⟩⁽¹⁾, c=|c⟩⁽¹⁾, zero=0, one=1, inner=<lambda>, adjoint=<lambda>, is_scalar=<lambda> | |
has_product: Exception OverlappingSpaces('(|a⟩⁽¹⁾, |b⟩⁽¹⁾)',) | |
has_left_distributive_law: Exception OverlappingSpaces('(|a⟩⁽¹⁾, |b⟩⁽¹⁾ + |c⟩⁽¹⁾)',) | |
has_right_distributive_law: Exception OverlappingSpaces('(|a⟩⁽¹⁾ + |b⟩⁽¹⁾, |c⟩⁽¹⁾)',) | |
has_product_associativity: Exception OverlappingSpaces('(|a⟩⁽¹⁾, |b⟩⁽¹⁾)',) | |
has_product_neutral_element: True | |
has_involution: True | |
has_adjoint_distribute_over_sum: Exception OverlappingSpaces('(|a⟩⁽¹⁾, |b⟩⁽¹⁾)',) | |
has_scalar_adjoint: True | |
has_cstar_identity: True | |
Checking Hilbert space properties for a=|a⟩⁽¹⁾, b=|b⟩⁽¹⁾, c=|c⟩⁽¹⁾, zero=0, inner=<lambda>, is_scalar=<lambda> | |
has_addition: True | |
has_scalar_product: True | |
has_add_associativity: True | |
has_add_commutativity: True | |
has_add_neutral_element: True | |
has_add_inverse: True | |
has_scalar_mul_compatibility: True | |
has_scalar_identity: True | |
has_distributivity: True | |
has_inner_product: True | |
*** Check for Kets on disjunct spaces *** | |
Checking C* Algebra properties for a=|a⟩⁽¹⁾, b=|b⟩⁽²⁾, c=|c⟩⁽³⁾, zero=0, one=1, inner=<lambda>, adjoint=<lambda>, is_scalar=<lambda> | |
has_product: True | |
has_left_distributive_law: Exception UnequalSpaces('[|b⟩⁽²⁾, |c⟩⁽³⁾]',) | |
has_right_distributive_law: Exception UnequalSpaces('[|a⟩⁽¹⁾, |b⟩⁽²⁾]',) | |
has_product_associativity: True | |
has_product_neutral_element: True | |
has_involution: True | |
has_adjoint_distribute_over_sum: True | |
has_scalar_adjoint: True | |
has_cstar_identity: True | |
Checking Hilbert space properties for a=|a⟩⁽¹⁾, b=|b⟩⁽²⁾, c=|c⟩⁽³⁾, zero=0, inner=<lambda>, is_scalar=<lambda> | |
has_addition: Exception UnequalSpaces('[|a⟩⁽¹⁾, |b⟩⁽²⁾]',) | |
has_scalar_product: True | |
has_add_associativity: Exception UnequalSpaces('[|b⟩⁽²⁾, |c⟩⁽³⁾]',) | |
has_add_commutativity: Exception UnequalSpaces('[|a⟩⁽¹⁾, |b⟩⁽²⁾]',) | |
has_add_neutral_element: True | |
has_add_inverse: True | |
has_scalar_mul_compatibility: True | |
has_scalar_identity: True | |
has_distributivity: Exception UnequalSpaces('[|a⟩⁽¹⁾, |b⟩⁽²⁾]',) | |
has_inner_product: Exception UnequalSpaces('(|a⟩⁽¹⁾, |b⟩⁽²⁾)',) | |
*** Check for Operators on same space *** | |
Checking C* Algebra properties for a=Â⁽¹⁾, b=B̂⁽¹⁾, c=Ĉ⁽¹⁾, zero=0, one=𝟙, inner=<lambda>, adjoint=<lambda>, is_scalar=<lambda> | |
has_product: True | |
has_left_distributive_law: True | |
has_right_distributive_law: True | |
has_product_associativity: True | |
has_product_neutral_element: True | |
has_involution: True | |
has_adjoint_distribute_over_sum: True | |
has_scalar_adjoint: True | |
has_cstar_identity: False | |
Checking Hilbert space properties for a=Â⁽¹⁾, b=B̂⁽¹⁾, c=Ĉ⁽¹⁾, zero=0, inner=<lambda>, is_scalar=<lambda> | |
has_addition: True | |
has_scalar_product: True | |
has_add_associativity: True | |
has_add_commutativity: True | |
has_add_neutral_element: True | |
has_add_inverse: True | |
has_scalar_mul_compatibility: True | |
has_scalar_identity: True | |
has_distributivity: True | |
has_inner_product: True | |
*** Check for Operators on disjunct spaces *** | |
Checking C* Algebra properties for a=Â⁽¹⁾, b=B̂⁽²⁾, c=Ĉ⁽³⁾, zero=0, one=𝟙, inner=<lambda>, adjoint=<lambda>, is_scalar=<lambda> | |
has_product: True | |
has_left_distributive_law: True | |
has_right_distributive_law: True | |
has_product_associativity: True | |
has_product_neutral_element: True | |
has_involution: True | |
has_adjoint_distribute_over_sum: True | |
has_scalar_adjoint: True | |
has_cstar_identity: False | |
Checking Hilbert space properties for a=Â⁽¹⁾, b=B̂⁽²⁾, c=Ĉ⁽³⁾, zero=0, inner=<lambda>, is_scalar=<lambda> | |
has_addition: True | |
has_scalar_product: True | |
has_add_associativity: True | |
has_add_commutativity: True | |
has_add_neutral_element: True | |
has_add_inverse: True | |
has_scalar_mul_compatibility: True | |
has_scalar_identity: True | |
has_distributivity: True | |
has_inner_product: True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment