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

WIP: improvements to TorchForwardSimulator #479

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
Next Next commit
tweak stateless_data
  • Loading branch information
rileyjmurray committed Jun 24, 2024
commit 4682043f66f68b388f40aa3d9037df6719ebdaf0
16 changes: 9 additions & 7 deletions pygsti/modelmembers/operations/fulltpop.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,18 @@ def from_vector(self, v, close=False, dirty_value=True):
self._ptr_has_changed() # because _rep.base == _ptr (same memory)
self.dirty = dirty_value

def stateless_data(self) -> Tuple[int]:
return (self.dim,)

@staticmethod
def torch_base(sd: Tuple[int], t_param: _torch.Tensor) -> _torch.Tensor:
dim = sd[0]
def stateless_data(self) -> Tuple[int, _torch.Tensor]:
dim = self.dim
t_const = _torch.zeros(size=(1, dim), dtype=_torch.double)
t_const[0,0] = 1.0
t_param_mat = t_param.reshape((dim - 1, dim))
return (dim, t_const)

@staticmethod
def torch_base(sd: Tuple[int, _torch.Tensor], t_param: _torch.Tensor) -> _torch.Tensor:
dim, t_const = sd
t_param_mat = t_param.view(dim - 1, dim)
t = _torch.row_stack((t_const, t_param_mat))
# TODO: cache the row of all zeros?
return t


Expand Down
23 changes: 11 additions & 12 deletions pygsti/modelmembers/povms/tppovm.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,28 @@ def to_vector(self):
vec = _np.concatenate(effect_vecs)
return vec

def stateless_data(self) -> Tuple[int, _np.ndarray]:
def stateless_data(self) -> Tuple[int, _torch.Tensor, int]:
num_effects = len(self)
complement_effect = self[self.complement_label]
identity = complement_effect.identity.to_vector()
return (num_effects, identity)

@staticmethod
def torch_base(sd: Tuple[int, _np.ndarray], t_param: _torch.Tensor) -> _torch.Tensor:
num_effects, identity = sd
identity = identity.reshape((1, -1)) # make into a row vector
t_identity = _torch.from_numpy(identity)

dim = identity.size

first_basis_vec = _np.zeros(dim)
first_basis_vec[0] = dim ** 0.25
first_basis_vec = _np.zeros((1,dim))
first_basis_vec[0,0] = dim ** 0.25
TOL = 1e-15 * _np.sqrt(dim)
if _np.linalg.norm(first_basis_vec - identity) > TOL:
# Don't error out. The documentation for the class
# clearly indicates that the meaning of "identity"
# can be nonstandard.
warnings.warn('Unexpected normalization!')
return (num_effects, t_identity, dim)

identity = identity.reshape((1, -1)) # make into a row vector
t_identity = _torch.from_numpy(identity)
t_param_mat = t_param.reshape((num_effects - 1, dim))
@staticmethod
def torch_base(sd: Tuple[int, _torch.Tensor, int], t_param: _torch.Tensor) -> _torch.Tensor:
num_effects, t_identity, dim = sd
t_param_mat = t_param.view(num_effects - 1, dim)
t_func = t_identity - t_param_mat.sum(axis=0, keepdim=True)
t = _torch.row_stack((t_param_mat, t_func))
return t
11 changes: 6 additions & 5 deletions pygsti/modelmembers/states/tpstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ def from_vector(self, v, close=False, dirty_value=True):
self._ptr_has_changed()
self.dirty = dirty_value

def stateless_data(self) -> Tuple[int]:
return (self.dim,)
def stateless_data(self) -> Tuple[_torch.Tensor]:
dim = self.dim
t_const = (dim ** -0.25) * _torch.ones(1, dtype=_torch.double)
return (t_const,)

@staticmethod
def torch_base(sd: Tuple[int], t_param: _torch.Tensor) -> _torch.Tensor:
dim = sd[0]
t_const = (dim ** -0.25) * _torch.ones(1, dtype=_torch.double)
def torch_base(sd: Tuple[_torch.Tensor], t_param: _torch.Tensor) -> _torch.Tensor:
t_const = sd[0]
t = _torch.concat((t_const, t_param))
return t

Expand Down