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

Change build_call_graph in bloqs to return dict #1392

Merged
merged 5 commits into from
Sep 7, 2024

Conversation

dstrain115
Copy link
Contributor

  • This changes the build_call_graph function within bloqs in qualtran to return a dictionary of cost counts rather than a set.
  • This will allow the ordering of cost counts to be deterministic

Note that this requires some slight code changes for bloqs that have multiple set items since (Toffoli(), 1) and (Toffoli(), 2) would have two different items in a set, but share an index in the dictionary.

This also may alter counts (i.e. fix a bug) where set items clobber each other. For instance, adding (Toffoli(), self.bits_a) and (Toffoli(), self.bits_b) will previously give the wrong count if bits_a == bits_b since the two items would be the same in the set.

- This changes the build_call_graph function within bloqs
in qualtran to return a dictionary of cost counts rather than
a set.
- This will allow the ordering of cost counts to be deterministic

Note that this requires some slight code changes for bloqs
that have multiple set items since (Toffoli(), 1) and (Toffoli(), 2)
would have two different items in a set, but share an index in the
dictionary.

This also may alter counts (i.e. fix a bug) where set items clobber
each other.  For instance, adding (Toffoli(), self.bits_a) and
(Toffoli(), self.bits_b) will previously give the wrong count
if bits_a == bits_b since the two items would be the same in the
set.
Comment on lines 84 to 93
cost[SignedIntegerToTwosComplement(self.num_bits_p)] += 3
cost[SignedIntegerToTwosComplement(self.num_bits_n)] += 3
# Adding nu into p / q. Nu is one bit larger than p.
cost_add_p = (Add(QInt(self.num_bits_p + 1)), 3)
cost_add_n = (Add(QInt(self.num_bits_n + 1)), 3)
cost_ctrl_add_p = (Toffoli(), 3 * (self.num_bits_p + 1))
cost_ctrl_add_n = (Toffoli(), 3 * (self.num_bits_n + 1))
cost[Add(QInt(self.num_bits_p + 1))] += 3
cost[Add(QInt(self.num_bits_n + 1))] += 3
cost[Toffoli()] += 3 * (self.num_bits_p + 1)
cost[Toffoli()] += 3 * (self.num_bits_n + 1)
# + 2 as these numbers are larger from addition of $\nu$
cost_inv_tc_p = (SignedIntegerToTwosComplement(self.num_bits_p + 2), 3)
cost_inv_tc_n = (SignedIntegerToTwosComplement(self.num_bits_n + 2), 3)
cost[SignedIntegerToTwosComplement(self.num_bits_p + 2)] += 3
cost[SignedIntegerToTwosComplement(self.num_bits_n + 2)] += 3
Copy link
Collaborator

Choose a reason for hiding this comment

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

i feel like there may be some information lost by removing those variable names? Not blocking, but maybe include in comments, eg the cost[Toffoli()] += 3 * self.num_bits_p is for ctrl_add_p which isn't otherwise obvious

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

# Cost is $5(n_{p} - 1) + 2$ which comes from copying each $w$ component of $p$
# into an ancilla register ($3(n_{p}-1)$), copying the $r$ and $s$ bit of into an
# ancilla ($2(n_{p}-1)$), controlling on both those bit perform phase flip on an
# ancilla $|+\rangle$ state. This requires $1$ Toffoli, Then erase which costs
# only Cliffords. There is an additional control bit controlling the application
# of $T$ thus we come to our total.
# Eq 73. page
return {(Toffoli(), (5 * (self.num_bits_p - 1) + 2))}
# Eq 73, page 20.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice

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.

wow! great

@mpharrigan
Copy link
Collaborator

didn't mean to conflict you. I'll try to fix them up quick

@dstrain115 dstrain115 merged commit 488b9a5 into quantumlib:main Sep 7, 2024
8 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.

2 participants