Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Tensor from classical action #1514

Merged
merged 18 commits into from
Jan 24, 2025

Conversation

anurudhp
Copy link
Contributor

@anurudhp anurudhp commented Dec 31, 2024

  • Compute the dense tensor using the bloq's classical action: fixes Tensors from classical action #691
  • my_tensors_from_classical_action: a drop-in replacement for bloq.my_tensors that authors of classical bloqs can call, to avoid manually defining the explicit tensor.
  • drive by fixes: add missing autotest for cswap example, reduce bitsize of comparator bloq example.

@anurudhp anurudhp marked this pull request as ready for review January 14, 2025 14:22
@anurudhp
Copy link
Contributor Author

@mpharrigan ptal!

Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

sweeeeeeet

A couple of nits.

Do you want to change some of the existing bloqs to use this method? Maybe And. This can be in a follow-up. I'm also curious in your exhaustive testing notebook. Maybe you can contribute that to dev_tools in a follow up

qualtran/simulation/tensor/tensor_from_classical.py Outdated Show resolved Hide resolved
qualtran/simulation/tensor/tensor_from_classical.py Outdated Show resolved Hide resolved
qualtran/simulation/tensor/tensor_from_classical.py Outdated Show resolved Hide resolved
for it when the bloq has a known classical action.

Examples:
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

did you check how this shows up in the docs? does it look ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this didn't work actually, so I removed the Examples:. works now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for checking! (I suspected the "Examples" heading wouldn't work)

n_qubits_left = sum(left_qubit_counts)
n_qubits_right = sum(reg.total_bits() for reg in bloq.signature.rights())

matrix = np.zeros((2,) * (n_qubits_right + n_qubits_left))
Copy link
Collaborator

Choose a reason for hiding this comment

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

consider adding a check with a helpful error message if n_qubits_right + n_qubits_left is an unreasonably large number

Copy link
Contributor Author

Choose a reason for hiding this comment

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

raises a value error when total > 40

try:
matrix = _bloq_to_dense_via_classical_action(bloq)
except ValueError as e:
raise ValueError(f"cannot compute tensor: {bloq} is not classical") from e
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm ValueError could potentially come from another source, no? Maybe include the original str(e) message in this error message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@anurudhp anurudhp requested a review from mpharrigan January 24, 2025 03:48
Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

thanks for the improvements!

@mpharrigan mpharrigan enabled auto-merge (squash) January 24, 2025 18:41
@mpharrigan mpharrigan merged commit 7bbec3b into quantumlib:main Jan 24, 2025
8 checks passed
@anurudhp anurudhp deleted the 2024/12/22-tensor-from-classical branch January 24, 2025 21:24
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tensors from classical action
2 participants