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

QOSF Screening tasks submission for Unitary fund Hackathon Tasks descriptions with possible solutions provided #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
279 changes: 279 additions & 0 deletions QOSFScreeningTasks (1).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"!pip install pennylane"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iB9ScKBz3Wny",
"outputId": "5d78f41d-6ecc-47d0-e8b4-906c74dd1738"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: pennylane in /usr/local/lib/python3.10/dist-packages (0.36.0)\n",
"Requirement already satisfied: numpy<2.0 in /usr/local/lib/python3.10/dist-packages (from pennylane) (1.25.2)\n",
"Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from pennylane) (1.11.4)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from pennylane) (3.3)\n",
"Requirement already satisfied: rustworkx in /usr/local/lib/python3.10/dist-packages (from pennylane) (0.14.2)\n",
"Requirement already satisfied: autograd in /usr/local/lib/python3.10/dist-packages (from pennylane) (1.6.2)\n",
"Requirement already satisfied: toml in /usr/local/lib/python3.10/dist-packages (from pennylane) (0.10.2)\n",
"Requirement already satisfied: appdirs in /usr/local/lib/python3.10/dist-packages (from pennylane) (1.4.4)\n",
"Requirement already satisfied: semantic-version>=2.7 in /usr/local/lib/python3.10/dist-packages (from pennylane) (2.10.0)\n",
"Requirement already satisfied: autoray>=0.6.1 in /usr/local/lib/python3.10/dist-packages (from pennylane) (0.6.12)\n",
"Requirement already satisfied: cachetools in /usr/local/lib/python3.10/dist-packages (from pennylane) (5.3.3)\n",
"Requirement already satisfied: pennylane-lightning>=0.36 in /usr/local/lib/python3.10/dist-packages (from pennylane) (0.36.0)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from pennylane) (2.31.0)\n",
"Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from pennylane) (4.11.0)\n",
"Requirement already satisfied: future>=0.15.2 in /usr/local/lib/python3.10/dist-packages (from autograd->pennylane) (0.18.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->pennylane) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->pennylane) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->pennylane) (2.0.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->pennylane) (2024.2.2)\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"QOSF Screening tasks submission for Unitary fund Hackathon\n",
"Tasks descriptions with possible solutions provided"
],
"metadata": {
"id": "TvK0R-Lu_QZu"
}
},
{
"cell_type": "markdown",
"source": [
"Task: Quantum-Inspired Integer Comparison\n",
"Problem Statement:\n",
"Given two integers, design a quantum circuit that compares them and outputs a binary result indicating which integer is greater.\n",
"\n",
"Requirements:\n",
"Implement a quantum circuit using any Quantum SDK that compares two integers.\n",
"The integers should be encoded in the amplitudes of qubits.\n",
"The circuit should output a binary result indicating which integer is greater (0 if the first integer is greater, 1 if the second integer is greater, and 2 if both integers are equal).\n",
"Example:\n",
"Input:\n",
"\n",
"Integer 1: 5\n",
"Integer 2: 7\n",
"Output:\n",
"\n",
"Binary result: 0 (indicating that integer 1 is greater than integer 2)"
],
"metadata": {
"id": "2arCl2BC50-J"
}
},
{
"cell_type": "code",
"source": [
"import pennylane as qml\n",
"from pennylane import numpy as np\n",
"\n",
"def encode_integer(integer, num_qubits):\n",
" \"\"\"\n",
" Encode an integer into the amplitudes of a set of qubits.\n",
" \"\"\"\n",
" binary_rep = \"{0:b}\".format(integer).zfill(num_qubits)\n",
" return [int(bit) for bit in binary_rep]\n",
"\n",
"def integer_comparison_circuit(int1, int2):\n",
" \"\"\"\n",
" Quantum circuit for comparing two integers.\n",
" \"\"\"\n",
" num_qubits = max(len(\"{0:b}\".format(int1)), len(\"{0:b}\".format(int2)))\n",
"\n",
" # Define the quantum device\n",
" dev = qml.device(\"default.qubit\", wires=num_qubits * 2)\n",
"\n",
" # Define the quantum circuit\n",
" @qml.qnode(dev)\n",
" def circuit():\n",
" # Encode the integers into the amplitudes of qubits\n",
" qml.BasisState(encode_integer(int1, num_qubits), wires=range(num_qubits))\n",
" qml.BasisState(encode_integer(int2, num_qubits), wires=range(num_qubits, 2 * num_qubits)) # Start from num_qubits\n",
"\n",
" # Apply quantum operations to compare the integers\n",
" for i in range(num_qubits):\n",
" qml.CNOT(wires=[i, i + num_qubits])\n",
"\n",
" # Measure the qubits to obtain the result\n",
" return [qml.expval(qml.PauliZ(wires=i)) for i in range(num_qubits)]\n",
"\n",
" # Execute the circuit and obtain the result\n",
" result = circuit()\n",
"\n",
" # Determine the binary result indicating which integer is greater\n",
" binary_result = \"\"\n",
" for val in result:\n",
" if val > 0:\n",
" binary_result += \"1\"\n",
" else:\n",
" binary_result += \"0\"\n",
"\n",
" # Return the binary result\n",
" return binary_result\n",
"\n",
"# Example usage\n",
"integer1 = 5\n",
"integer2 = 7\n",
"result = integer_comparison_circuit(integer1, integer2)\n",
"print(\"Binary result:\", result)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OxhIkHQ45s4x",
"outputId": "1f943234-b8b6-47c2-c76a-37f139018902"
},
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Binary result: 010\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Quantum-Inspired Function Optimization\n",
"\n",
"Problem Statement:\n",
"The task involves optimizing a mathematical function using a quantum-inspired algorithm. This optimization aims to find either the minimum or maximum value of the objective function over a specified range of input variables.\n",
"\n",
"Requirements:\n",
"\n",
"Choose a Function: Select a mathematical function that serves as the objective function to be optimized. This function should be parameterized by one or more variables.\n",
"\n",
"Quantum Circuit Ansatz: Design a quantum circuit ansatz that parameterizes the quantum state preparation. This ansatz should depend on the parameters to be optimized and the input variables of the objective function.\n",
"\n",
"Quantum Cost Function: Define a quantum cost function that evaluates the expectation value of an observable operator on the quantum circuit output. This cost function will be optimized to minimize or maximize the objective function.\n",
"\n",
"Classical Optimization: Choose a classical optimization algorithm, such as gradient descent, to optimize the parameters of the quantum circuit ansatz based on the quantum cost function's evaluation.\n",
"\n",
"Optimization Routine: Implement a routine to optimize the quantum circuit parameters iteratively. This routine should update the parameters using the classical optimization algorithm until convergence is reached.\n",
"\n",
"Evaluate Results: Test the quantum-inspired optimization algorithm with different functions and analyze its performance. Compare the optimized function values obtained using the quantum-inspired algorithm with classical optimization methods.\n",
"\n",
"Bonus Tasks:\n",
"\n",
"Performance Analysis: Conduct a comparative analysis of the quantum-inspired optimization algorithm with classical optimization algorithms in terms of convergence speed, accuracy, and scalability.\n",
"\n",
"Noise Handling: Extend the optimization algorithm to handle noise and errors in quantum devices. Implement error mitigation techniques to enhance the optimization algorithm's robustness.\n",
"\n",
"Parameter Initialization: Explore different strategies for initializing the quantum circuit parameters and evaluate their impact on optimization performance.\n",
"\n",
"Hybrid Approaches: Investigate hybrid quantum-classical optimization techniques, where classical optimization algorithms are combined with quantum circuits to achieve better optimization results.\n",
"\n",
"Extension to Multi-variable Functions: Generalize the optimization algorithm to handle functions with multiple variables. Design quantum circuit ansatz and cost functions suitable for multi-variable optimization tasks."
],
"metadata": {
"id": "JbeaYZf0-QqV"
}
},
{
"cell_type": "code",
"source": [
"import pennylane as qml\n",
"from pennylane import numpy as np\n",
"\n",
"# Define the function to optimize\n",
"def my_function(x):\n",
" return x**2 - 4*x + 4 # Example function: f(x) = x^2 - 4x + 4\n",
"\n",
"# Define the quantum device\n",
"dev = qml.device(\"default.qubit\", wires=1)\n",
"\n",
"# Define the quantum circuit ansatz\n",
"def ansatz(params, x):\n",
" qml.Rot(*params[0], wires=0)\n",
" qml.RX(x, wires=0)\n",
"\n",
"# Define the quantum cost function\n",
"@qml.qnode(dev)\n",
"def cost(params, x):\n",
" ansatz(params, x)\n",
" return qml.expval(qml.PauliZ(0))\n",
"\n",
"# Define the VQE optimizer\n",
"optimizer = qml.GradientDescentOptimizer(stepsize=0.1)\n",
"\n",
"# Define the function optimization routine\n",
"def optimize_function(x):\n",
" num_steps = 100\n",
" params = np.random.randn(1, 3) # Initialize parameters randomly\n",
"\n",
" for _ in range(num_steps):\n",
" # Optimize parameters\n",
" params = optimizer.step(lambda v: (cost(v, x),), params)\n",
"\n",
" return params\n",
"\n",
"# Define the range of values for the variable x\n",
"x_range = np.linspace(0, 4, 100)\n",
"\n",
"# Optimize the function for each value of x in the range\n",
"minimum_value = float('inf')\n",
"optimal_x = None\n",
"\n",
"for x_val in x_range:\n",
" params = optimize_function(x_val)\n",
" optimized_value = my_function(x_val)\n",
" if optimized_value < minimum_value:\n",
" minimum_value = optimized_value\n",
" optimal_x = x_val\n",
"\n",
"print(\"Optimal value of x:\", optimal_x)\n",
"print(\"Minimum value of the function:\", minimum_value)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7FXB79089xTm",
"outputId": "85fd1b3f-4abb-4ffb-b8a8-71e1131cfa7b"
},
"execution_count": 36,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Optimal value of x: 1.97979797979798\n",
"Minimum value of the function: 0.0004081216202429516\n"
]
}
]
}
]
}