Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
241 changes: 241 additions & 0 deletions .ipynb_checkpoints/QuantumPhaseEstimation-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile\n",
"from qiskit_aer import AerSimulator\n",
"from qiskit.quantum_info import Operator\n",
"from qiskit.visualization import plot_histogram\n",
"import numpy as np\n",
"import math"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"pi=np.pi"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def qft_rot(circuit, n):\n",
" if n == 0:\n",
" return circuit\n",
" \n",
" n = n-1\n",
" circuit.h(n)\n",
" for i in range(n):\n",
" circuit.cp(pi/(2**(n-i)), i, n)\n",
" \n",
" qft_rot(circuit, n)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def iqft_rot(circuit, n):\n",
" for i in range(n):\n",
" if i > 0:\n",
" for j in range(i, 0, -1):\n",
" circuit.cp(-pi/(2**j), i, i-j)\n",
" circuit.h(i) \n",
" return circuit"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"def phase_estimation(oracle, eigenstate, number_qubits):\n",
" n = number_qubits\n",
" qr = QuantumRegister(n+1, 'q')\n",
" c = ClassicalRegister(n, 'c')\n",
" qc = QuantumCircuit(qr, c)\n",
" \n",
" if eigenstate == 1:\n",
" qc.x(n)\n",
" \n",
" for i in range(n//2):\n",
" qc.swap(i, n-i-1)\n",
" \n",
" for i in range(3):\n",
" qc.h(i) \n",
" \n",
" cGate = oracle.control(1)\n",
" qc.barrier()\n",
" \n",
" for i in range(n):\n",
" for j in range(2**i):\n",
" qc.append(cGate, [i, n])\n",
" \n",
" qc.barrier()\n",
" iqft_rot(qc, n+1)\n",
" qc.barrier()\n",
" \n",
" for i in range(n):\n",
" qc.measure(i, i)\n",
" \n",
" aersim = AerSimulator(shots=1000)\n",
" circuit_transpile = transpile(qc, aersim)\n",
" result = aersim.run(circuit_transpile).result()\n",
" counts = result.get_counts()\n",
" \n",
" # Get the most common bitstring and convert to phase\n",
" most_common_bitstring = max(counts, key=counts.get)\n",
" N = len(most_common_bitstring)\n",
" phase = int(most_common_bitstring, 2) / 2**N\n",
" \n",
" hist = plot_histogram(counts, title=f\"Phase Estimation for |{1}>\")\n",
" \n",
" return counts, phase, qc"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def create_gate_from_matrix(matrix):\n",
" \"\"\"\n",
" Create a quantum circuit from a nxn unitary matrix\n",
" \"\"\"\n",
" n = matrix.shape[0]\n",
" n_qubits = int(math.log2(n))\n",
" qc = QuantumCircuit(1) # We only need 1 qubit for 2x2 matrix\n",
" \n",
" op = Operator(matrix)\n",
" qc.unitary(op, [0], label='L')\n",
" \n",
" return qc"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"def analyze_phases(counts, n_qubits, zero_threshold=1e-3):\n",
" \"\"\"\n",
" Analyze phases from measurement results and count zero phases\n",
" \"\"\"\n",
" total_shots = sum(counts.values())\n",
" phases = {}\n",
" zero_phase_count = 0\n",
" \n",
" for bitstring, count in counts.items():\n",
" phase = int(bitstring, 2) / (2**n_qubits)\n",
" probability = count / total_shots\n",
" phases[phase] = probability\n",
" \n",
" if phase < zero_threshold:\n",
" zero_phase_count += 1\n",
" \n",
" return phases, zero_phase_count"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def analyze_matrix(matrix, n_qubits=4, zero_threshold=1e-3):\n",
" \"\"\"\n",
" Analyze a matrix using QPE to detect zero eigenvalues\n",
" \"\"\"\n",
" \n",
" # Create gate from matrix\n",
" gate = create_gate_from_matrix(matrix)\n",
" \n",
" # Run phase estimation\n",
" counts, estimated_phase, _ = phase_estimation(gate, 1, n_qubits)\n",
" \n",
" # Analyze phases and count zeros\n",
" phases, zero_count = analyze_phases(counts, n_qubits, zero_threshold)\n",
" \n",
" print(f\"\\nResults:\")\n",
" print(f\"Estimated primary phase: {estimated_phase:.4f}\")\n",
" print(f\"Number of zero phases detected: {zero_count}\")\n",
" \n",
" return zero_count"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Analyzing matrix:\n",
"[[0.99500417-0.09983342j 0. +0.j ]\n",
" [0. +0.j 0.99500417-0.09983342j]]\n",
"\n",
"Results:\n",
"Estimated primary phase: 0.0000\n",
"Number of zero phases detected: 1\n",
"\n",
"Final result: 1 zero eigenvalues detected\n"
]
}
],
"source": [
"L = np.array([[0.99500417-0.09983342j, 0],\n",
" [0, 0.99500417-0.09983342j]])\n",
"\n",
"print(\"Analyzing matrix:\")\n",
"print(L)\n",
"n_zeros = analyze_matrix(L)\n",
"print(f\"\\nFinal result: {n_zeros} zero eigenvalues detected\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
70 changes: 70 additions & 0 deletions .ipynb_checkpoints/README-checkpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# iQuHACK 2025 - Moody's Challenge

Here you will learn the details that are needed in order to access and operate resources for this challenge. See **moodys_challenge** to read the challenge. Make sure to first read the instructions below.

## Working on qBraid
[<img src="https://qbraid-static.s3.amazonaws.com/logos/Launch_on_qBraid_white.png" width="150">](https://account.qbraid.com?gitHubUrl=https://github.com/iQuHACK/2025-Moodys.git)

While simulations and emulations of your program can be done locally on your computer, the Moody's challenge will require access to qBraid for quantum hardware.

So here are some guidelines:
1. To launch these materials on qBraid, first fork this repository and click the above `Launch on qBraid` button. It will take you to your qBraid Lab with the repository cloned.
2. Once cloned, open terminal (click **FILES** in the left sidebar, then click the blue button with a symbol "➕", it is the first icon in the **Other** column in Launcher) and `cd` into this repo. Set the repo's remote origin using the git clone url you copied in Step 1, and then create a new branch for your team:

```bash
cd 2025-Moodys
git remote set-url origin <url>
git branch <team_name>
git checkout <team_name>

```

3. <img align="right" width="43%" src="./assets/Screenshot 2025-01-30 at 22.17.47.png">Use the environment manager (**ENVS** tab in the right sidebar) to [install environment](https://docs.qbraid.com/cli/user-guide/environments) "Moody's IQuHACK 2025". The installation should take ~2 min.
4. Once the installation is complete, go back to the Launcher to [add a new ipykernel](https://docs.qbraid.com/lab/user-guide/kernels) for "Moody's".
5. From the **FILES** tab in the left sidebar, double-click on the `2024_Moodys` directory.
6. You are now ready to begin hacking, [submitting jobs](https://docs.qbraid.com/lab/user-guide/quantum-jobs)! Work with your team to complete the challenge listed above.

Please note, you will be provisioned credits before the hackathon for quantum hardwares. The following resources are provided in this challenge:

* IonQ Aria 1 Noisy Quantum Simulator (no time limit, [qiskit syntax](#Syntax-of-using-IonQ-simulator-on-qiskit-circuit), [other syntax](https://docs.qbraid.com/sdk/user-guide/providers/ionq))
* IBM hardware (10 minutes, [syntax](https://docs.qbraid.com/sdk/user-guide/providers/ibm))

**Strategize carefully and conduct back of the envelope estimates for your experiments before running**.

For other questions or additional help using qBraid, see [Lab User Guide](https://docs.qbraid.com/projects/lab/en/latest/lab/overview.html), or reach out on the IQuHack qBraid Slack Channel.

## Before submission

Make sure that you devote some time to prepare a brief presentation (3-7 mins) showing your work. This presentation will be presented on Sunday.

We encourage you to show your experimental results, and your innovative solutions.

## Syntax of using IonQ simulator on qiskit circuit

Qiskit circuit is used as an example here.

```python
from qbraid.programs import load_program
from qbraid.runtime import QbraidProvider
from qbraid.transpiler.conversions.qiskit import qiskit_to_ionq

provider = QbraidProvider()

device = provider.get_device("ionq_simulator")

ionq_dict = qiskit_to_ionq(circuit, gateset="native")

program = load_program(ionq_dict)

run_input = program.serialize()

job = device.submit(run_input, shots=100, noise_model="aria-1")
```

## Scoring Criteria

The scoring criteria are as follows:

- **55%** is based on the main quantum TDA workflow (steps 1~4).
- **40%** is allocated to creativity and innovation, evaluated through the work on one of challenge from the BONUS section.
- **5%** is assigned to presentation quality.
Loading