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

Controlled Bloq learns add_my_tensors #815

Merged
merged 26 commits into from
Mar 26, 2024

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Mar 21, 2024

Fixes #650

Currently based on top of #809

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])
Copy link
Collaborator

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?

Copy link
Collaborator Author

@tanujkhattar tanujkhattar Mar 22, 2024

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()

Comment on lines 406 to 408
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):
Copy link
Collaborator

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.

Copy link
Collaborator Author

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"

Copy link
Collaborator Author

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.

Comment on lines 181 to 192
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):
Copy link
Collaborator

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

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)
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@tanujkhattar
Copy link
Collaborator Author

@mpharrigan I've addressed all comments, PTAL

Comment on lines +65 to +67
"""Returns the "active" subspace corresponding to `signature` and `ctrl_spec`.

Assumes first n-registers for `signature` are control registers corresponding to `ctrl_spec`.
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@tanujkhattar tanujkhattar enabled auto-merge (squash) March 26, 2024 18:19
@tanujkhattar tanujkhattar merged commit a0ba6e9 into quantumlib:main Mar 26, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[controlled] Use quimb tensor factorization for Controlled bloqs
2 participants