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

Add initial demo notebook and codespace config #1

Merged
merged 3 commits into from
Mar 12, 2025
Merged

Conversation

bachase
Copy link
Collaborator

@bachase bachase commented Mar 7, 2025

This repository and initial comment fixes unitaryfoundation/ucc#296.

I decided to go with a separate repository over adding directly to the ucc repo to keep management cleaner and avoid it interfering with normal ucc development workflow.

For now, I did need to put a development version of ucc in the requirements.txt file until a version with unitaryfoundation/ucc#293 is released.

You should be able to try out the Github codespace, by selecting this initial-prototype branch, clicking the code/codespace to launch one for this branch.
image

I'm not yet sure how codespace caches the images, but for the first time the codespace was setup, it took a few minutes to install the right version of python and the requirements. If that is the same for others, I can look into speeding it up.

Copy link

@willzeng willzeng left a comment

Choose a reason for hiding this comment

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

This is great! This has a good walkthrough structure and it's going to be a great user experience in the end.

Had a few bugs in my run through that are commented here.

Also, codespaces defaulted to recommend that I use the Python 3.12.8 kernel which then didn't have ucc installed. When I switched it to Python 3.12.9 manually the notebook worked. This fix may not have been obvious for a user at the start and could add friction. Perhaps there is a way to avoid that default or to install ucc on a few versions in case?

Comment on lines +111 to +110
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cirq\n",
"\n",
"qubits = cirq.LineQubit.range(2)\n",
"cirq_circuit = cirq.Circuit(\n",
" cirq.H(qubits[0]),\n",
" cirq.CNOT(qubits[0], qubits[1])\n",
")\n",
"cirq_compiled_circuit = compile(cirq_circuit)\n",
"\n",
"print(cirq_circuit)\n",
"print(\"------------------------\")\n",
"print(cirq_compiled_circuit)"
]
},

Choose a reason for hiding this comment

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

This cell resulted in this error for me:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 8
      3 qubits = cirq.LineQubit.range(2)
      4 cirq_circuit = cirq.Circuit(
      5     cirq.H(qubits[0]),
      6     cirq.CNOT(qubits[0], qubits[1])
      7 )
----> 8 cirq_compiled_circuit = compile(cirq_circuit)
     10 print(cirq_circuit)
     11 print("------------------------")

TypeError: compile() missing required argument 'filename' (pos 2)

Comment on lines +148 to +146
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from qiskit.circuit.random import random_clifford_circuit\n",
"input_gates = [\"cx\", \"cz\", \"cy\", \"swap\", \"x\", \"y\", \"z\", \"s\", \"sdg\", \"h\"]\n",
"num_qubits = 10\n",
"random_circuit = random_clifford_circuit(\n",
" num_qubits, gates=input_gates, num_gates=10 * num_qubits * num_qubits\n",
")\n",
"compiled_random_circuit = compile(random_circuit)\n",
"print(f\"Number of multi-qubit gates in original circuit: {random_circuit.num_nonlocal_gates()}\")\n",
"print(f\"Gates used in original circuit: {random_circuit.count_ops()}\")\n",
"print(f\"Number of multi-qubit gates in compiled circuit: {compiled_random_circuit.num_nonlocal_gates()}\")\n",
"print(f\"Gates used in compiled circuit: {compiled_random_circuit.count_ops()}\")"
]
},

Choose a reason for hiding this comment

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

This gave error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 7
      3 num_qubits = 10
      4 random_circuit = random_clifford_circuit(
      5     num_qubits, gates=input_gates, num_gates=10 * num_qubits * num_qubits
      6 )
----> 7 compiled_random_circuit = compile(random_circuit)
      8 print(f"Number of multi-qubit gates in original circuit: {random_circuit.num_nonlocal_gates()}")
      9 print(f"Gates used in original circuit: {random_circuit.count_ops()}")

TypeError: compile() missing required argument 'filename' (pos 2)

Choose a reason for hiding this comment

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

These errors happened again in other cells but will suppress making the same comment

@bachase
Copy link
Collaborator Author

bachase commented Mar 11, 2025

This is great! This has a good walkthrough structure and it's going to be a great user experience in the end.

Had a few bugs in my run through that are commented here.

Also, codespaces defaulted to recommend that I use the Python 3.12.8 kernel which then didn't have ucc installed. When I switched it to Python 3.12.9 manually the notebook worked. This fix may not have been obvious for a user at the start and could add friction. Perhaps there is a way to avoid that default or to install ucc on a few versions in case?

Thanks @willzeng -- I'll play some more with the codespace settings to avoid having to do that and the added friction with the different ucc version.

@bachase bachase force-pushed the initial-prototype branch from 87af58c to 1d24f59 Compare March 11, 2025 14:22
@bachase bachase force-pushed the initial-prototype branch from 16a3d4c to ff64b14 Compare March 11, 2025 14:41
@jordandsullivan
Copy link

jordandsullivan commented Mar 12, 2025

Thanks @bachase . I got the codespace to work after waiting a few minutes for it to spin up and then restarting the jupyter kernel.

Bell state

So I know that the Bell state generation code snippet is taken from our README, but practically speaking that was just meant to show that you can switch between frontends easily. Nathan was originally going to update those code snippets in unitaryfoundation/ucc#203 to be a bit more compelling.

Long story short, a Bell state (containing just H and CNOT) shouldn't get compiled down to anything else, so not really compelling to demonstrate UCC's performance.

And in fact here (as well as the TKET case), UCC is inserting extra gates where none are needed, which may be something we want to investigate:
Screenshot 2025-03-11 at 4 56 52 PM

Random Clifford

On the random clifford circuit, it is probably worth emphasizing that the UCC compiled number is actually 2-qubit gates (since the target gate set only includes cx and no higher qubit-count gates), while the number of gates generated in the random clifford can include gates on more than 2 qubits. This will help clarify why the number of compiled gates is higher than uncompiled.
Screenshot 2025-03-11 at 4 56 02 PM

This issue was meant to break those into separate counting functions unitaryfoundation/ucc#46, but we decided to simply decompose our benchmark circuits into 2 qubit gates before running each of the compile functions, to have a shared starting point. Probably also worth mentioning that different devices have different native gatesets, so decomposition is necessary.

Custom passes

Given that we are trying to give this demo tomorrow to folks who have compiler expertise, perhaps it is better to focus on diving into how you would implement a custom pass and focus less on performance. We could ask what their normal workflow looks like for implementing new passes and try to figure out how they would map onto UCC.

@bachase
Copy link
Collaborator Author

bachase commented Mar 12, 2025

Thanks for your review and feedback @jordandsullivan!

  1. Bell state Good catch on what should've been a no-op on the Bell state circuit. I'll investigate that an open a corresponding issue in ucc repo
  2. Random Clifford I can add that detail to the text in the notebook. Can you confirm which gates in the generated clifford circuit are acting on more than 2 qubits? It looks like the target is all 1 or 2 qubit gates in the example. But agreed that we have a more restrictive target basis in the compilation.
  3. Custom Passes Yes, open to that being a focal point. I admit, I'm still unclear on how to frame the value prop for compiler pass writers here, so will be good to talk that out loud in the demo.

@bachase
Copy link
Collaborator Author

bachase commented Mar 12, 2025

Pushed an update to address #2 and now uses the latest custom pass approach as discussed in unitaryfoundation/ucc#292

Copy link
Member

@nathanshammah nathanshammah left a comment

Choose a reason for hiding this comment

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

Thanks @bachase and @jordandsullivan.

The notebook looks very nice to me, modulo Jordan's comments. It ran fine in Codespaces now with the new commit.

"You'll notice a much bigger change in this circuit, but again compiling to the default target gateset": I understand that you mean that here the number of gates increased? I would say it explicitly.

@jordandsullivan yes thanks for the reminder on the circuits issue.

@bachase bachase force-pushed the initial-prototype branch from 8865ccb to 4c325a3 Compare March 12, 2025 14:23
@bachase
Copy link
Collaborator Author

bachase commented Mar 12, 2025

"You'll notice a much bigger change in this circuit, but again compiling to the default target gateset": I understand that you mean that here the number of gates increased? I would say it explicitly.

Snuck in a fix for that.

@jordandsullivan
Copy link

Looks good @bachase . Thanks for addressing these and creating corresponding issues; I like the new framing.

@bachase bachase merged commit 5c34105 into main Mar 12, 2025
@willzeng
Copy link

willzeng commented Mar 12, 2025 via email

@bachase bachase deleted the initial-prototype branch March 12, 2025 21:22
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.

Create a demo notebook for users to take UCC for a spin
4 participants