-
Notifications
You must be signed in to change notification settings - Fork 50
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
Controlled
Bloq learns add_my_tensors
#815
Controlled
Bloq learns add_my_tensors
#815
Conversation
qualtran/_infra/controlled.py
Outdated
for cv, ctrl_reg in zip(self.ctrl_spec.cvs, self.ctrl_regs): | ||
for idx in ctrl_reg.all_idxs(): | ||
active_idx[ctrl_idx] = int(cv[idx]) | ||
active_idx[ctrl_idx + len(out_ind)] = int(cv[idx]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is going on here? what's the + len(out_ind)
? should active_idx
be two dimensional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We typically pass out_idx + inp_idx
to the tn.add()
call. The active_idx
is a tuple of length len(out_idx.shape) + len(inp_idx.shape)
; which is equal to len(data.shape)
and is used to find the subspace of data
(an n-d array) that should be actived.
Thinking of CSWAP; if you want to express data[1, :, :, 1, :, :] = _swap_matrix()
; active_idx
will be a tuple corresponding to (1, :, :, 1, :, :)
so you can do data[active_idx] = _swap_matrix()
qualtran/_infra/controlled.py
Outdated
active_idx: List[Union[int, slice]] = [slice(x) for x in data.shape] | ||
ctrl_idx = 0 | ||
for cv, ctrl_reg in zip(self.ctrl_spec.cvs, self.ctrl_regs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if I understand correctly, this is highly specific to the current concrete implementation of CtrlSpec
. You can imagine a world where there is an abstract CtrlSpec
base class and multiple implementations that can support things other than conjunction of a bunch of equals-a-value. In my head, cvs
would be an implementation detail and CtrlSpec.is_active
would be the interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At that point, we could iterate over all 2 ** n
possible bitstrings that cvs
can take and call is_active
to check if that subspace should be "Activated"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note SumOfProducts
is a generalized representation that can be used to represent any arbitrary boolean function.
def _tensor_shape_from_signature(signature: Signature) -> Tuple[int, ...]: | ||
return tuple( | ||
[ | ||
*itertools.chain.from_iterable( | ||
(2**reg.bitsize,) * int(np.prod(reg.shape)) | ||
for reg in [*signature.rights(), *signature.lefts()] | ||
) | ||
] | ||
) | ||
|
||
|
||
def _tensor_data_from_unitary_and_signature(inp_unitary: np.ndarray, signature: Signature): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to a module in qualtran.simulation.tensor
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
qualtran/_infra/controlled.py
Outdated
ctrl_idx += 1 | ||
# Put the subbloq tensor at indices where ctrl is active. | ||
subbloq_shape = _tensor_shape_from_signature(self.subbloq.signature) | ||
data[tuple(active_idx)] = self.subbloq.tensor_contract().reshape(subbloq_shape) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider factoring out a lot of this into a function in qualtran.simulation.tensor
. Perhaps: creating the data
array; and then a function that uses it to mangle the indices
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@mpharrigan I've addressed all comments, PTAL |
"""Returns the "active" subspace corresponding to `signature` and `ctrl_spec`. | ||
|
||
Assumes first n-registers for `signature` are control registers corresponding to `ctrl_spec`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
say somewhere that this is a sequence of indices/slices that can address into a ndarray
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Fixes #650
Currently based on top of #809