matt-lourens/hierarqcal

Quantum Circuit Tutorial: Modular Addition and Subtraction using HierarQcal

Opened this issue · 13 comments

Issue: Create a quantum computing tutorial in a Jupyter notebook to demonstrate modular arithmetic (addition and subtraction) using the Python package HierarQcal.

Overview

The goal is to provide a jupyter notebook tutorial that implements modular addition and subtraction within quantum circuits. The idea is that this will showcase some core hierarQal functionality. I have provided a code snippet modular_arithmetic_mvp.py that implements an mvp (minimum viable product) version, but it needs to be expanded and improved for pedagodgy, through refactoring, explanations and visualizations. The code follows the implementation from the Vedral et al. paper which will be a useful reference in understanding the circuit layout. Here's some example input and outputs which you can verify with the code snippet.

Addition:

hierq = Qinit(nq, tensors=tensors) + addition_mod_n # a+b % N
result_tensor = hierq()
get_results(result_tensor, bit_layout)
del result_tensor # just because it can be big
# input
a - 6
b - 3
N - 5
# output (answer is stored in b)
a: ['0', '1', '1']
b: ['0', '0', '1'] + ['0']
c: ['0', '0', '0']
n: ['1', '0', '1']
t: ['0']

$(6+3) \text{Mod} 5 = 4$ which is encoded in b: ['0', '0', '1'] + ['0'] (most significant bit is on the right, so this translates to 100 = 4)

# input
a - 4
b - 3
N - 4
# output (answer is stored in b)
a: ['0', '0', '1']
b: ['1', '1', '0'] + ['0']
c: ['0', '0', '0']
n: ['0', '0', '1']
t: ['0']

$(4+3) \text{Mod} 4 = 3$ which is encoded in b: ['1', '1', '0'] + ['0'] (most significant bit is on the right, so this translates to 011 = 4).

You can change things like the bit order (for display the most significant bit should be on the left, the reason for the above was that it was quickly implemented straight from the Vedral et al. Paper.). You'll see in the code there are uncommented x and r variables, these are for use in modular exponentiation, which is out of the scope for this issue but if you do find the above easy, you're welcome to extend the tutorial to modular exponentiation (let me know, I can then provide some more code that might be useful).

When debugging the code, it is useful to break things into parts, for example in looking what addition_mod_n does first run

hierq = (
    Qinit(nq, tensors=tensors)
    + addition
)
result_tensor = hierq()
get_results(result_tensor, bit_layout)
del result_tensor
plot_circuit(hierq, plot_width=100)

then

hierq = (
    Qinit(nq, tensors=tensors)
    + addition
    + swap_an
)
result_tensor = hierq()
get_results(result_tensor, bit_layout)
del result_tensor
plot_circuit(hierq, plot_width=100)

and so on.

Resources:

  • modular_arithmetic_example.py: The provided code snippet offers a minimal implementation for modular addition and subtraction circuits. Your task will be to expand on this code, providing explanations, visualizations and improvements for pedagogy.

  • Paper: Vedral et al. (1996), "Quantum Networks for Elementary Arithmetic Operations," provides details on modular arithmetic in quantum networks. It serves as the reference for implementing modular addition and subtraction circuits.

Requirements

  • Jupyter Notebook: Create a Jupyter notebook that demonstrates and explains modular addition and subtraction using HierarQcal.

Hi @matt-lourens while running your example here modular.py
it throws an error

--> 120 carry = lambda r: carry_motif if r == 1 else carry_motif.reverse()
    121 
    122 sum = lambda r=1: Qinit(3) + Qpivot(

AttributeError: 'Qhierarchy' object has no attribute 'reverse'```

Hi @matt-lourens even while print the modular circuit for my code the error is the same as above any fix for this

Hey @AbdullahKazi500, you need to install hierarqcal from the develop branch (i.e. pip install -e . /path/to/hierarqcal) with hierarqcal cloned and checked out on develop. This ensures you have the latest features for development, the code snippet should then work.

@matt-lourens removed pip
and added some detailed explanation for modular arithmetic

hi @AbdullahKazi500 and @matt-lourens ,

I'm a bit late to the game, but had been working on this bounty before I saw Abdullah's submission.

TL;DR I think our two approaches nicely complement each other and wanted to ask @AbdullahKazi500 if you'd want to combine our entries into a joint submission?

A bit more about my approach: I went about restructuring the notebook in a pedagogical fashion by giving some motivation for why the quantum approach to modular arithmetic is different from the classical case, and explain in detail how classical adders work to give some intuition.

If @AbdullahKazi500 you are interested, you could combine my structured notebook/section where I give intution for the problem with your nice explanations for the functions, and we'd solve/split the bounty together?

Let me know your thoughts - I understand if you prefer to work alone as well.

@khnikhil Hi Nikhil it is Okay for me I would be happy to work with you on this and split the bounty Thanks for the contribution

Also @khnikhil if you want we can tackle some other issues as well let me know how you want to connect so that we can discuss
and also for other such collaborations I am eager to work with you

hi @AbdullahKazi500 and @matt-lourens ,

I'm a bit late to the game, but had been working on this bounty before I saw Abdullah's submission.

TL;DR I think our two approaches nicely complement each other and wanted to ask @AbdullahKazi500 if you'd want to combine our entries into a joint submission?

A bit more about my approach: I went about restructuring the notebook in a pedagogical fashion by giving some motivation for why the quantum approach to modular arithmetic is different from the classical case, and explain in detail how classical adders work to give some intuition.

If @AbdullahKazi500 you are interested, you could combine my structured notebook/section where I give intution for the problem with your nice explanations for the functions, and we'd solve/split the bounty together?

Let me know your thoughts - I understand if you prefer to work alone as well.

Hey @khnikhil, I had a quick look at your draft and it looks great! The visualizations and focus on pedagogy is exactly what I was thinking of. I'll have time this weekend to go through the draft in more detail and yeah if you guys want to combine efforts and share the bounty, feel free to do so.

@AbdullahKazi500 thanks for the added explanations, it's much more in line of what I was hoping we can get out of this issue.

I'm excited to see the final version, reach out to me if there's any issues.

hi @AbdullahKazi500 and @matt-lourens - please forgive my terribly late response!

For some reason I didn't get a notification when you sent your message, and work was so unexpectedly hectic this past week that I didn't have a chance to work more on this.

I don't think I will have time to contribute more before the hackathon ends, but @AbdullahKazi500 please feel free to use my code in your response if you decide to go ahead with the challenge, and best of luck.

hi @AbdullahKazi500 and @matt-lourens - please forgive my terribly late response!

For some reason I didn't get a notification when you sent your message, and work was so unexpectedly hectic this past week that I didn't have a chance to work more on this.

I don't think I will have time to contribute more before the hackathon ends, but @AbdullahKazi500 please feel free to use my code in your response if you decide to go ahead with the challenge, and best of luck.

Hi Thanks Nikhil @matt-lourens Matt I have updated the final notebook combining both my and Nikhils work also I have worked on the comment made by you @matt-lourens on Nikhils PR and addressed it also provided references at the end - added the full adder image which is bigger
I have made the PR in the same PR patch Here PR

Hey @khnikhil, no worries and thanks again for the effort. I really did like the way it was going, maybe some day we can finish it up together.

@AbdullahKazi500 I looked at your PR and it does not resolve the issue.

So this bounty will not get claimed (unless there's a last last minute PR from someone). The issue will stay open, but just as a normal hierarQcal issue.

Hi @matt-lourens Okay thanks a lot