-
Notifications
You must be signed in to change notification settings - Fork 946
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
MAHOUT-468 & MAHOUT-469 Add parameter object and bind at execution #472
Changes from all commits
0b096f9
a65168a
c9e2d90
7240790
6569b2f
bdb3dc8
108adc3
701eefa
1776657
daba467
72a09e5
eb5fae7
63b1597
a231c3f
ea3a57b
db89a86
e670a7b
5ead65e
575bd82
e9ade55
b1b1f41
03ba85a
5b10819
af16248
308bc37
a10bab4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 2, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# pip install git+https://github.com/apache/mahout.git@main" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "lNDTZhztd2dp", | ||
"outputId": "ea3b9e41-43a8-44e7-9daf-e62e71d93143" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Quantum Circuit Parameterization and Optimization\n", | ||
"\n", | ||
"In this notebook, we'll explore how to create parameterized quantum circuits using the QuMat framework. This feature allows us to bind values to parameters at execution time, paving the way for optimization tasks in quantum computing.\n", | ||
"\n", | ||
"## Overview\n", | ||
"\n", | ||
"1. **Parameter Handling**: We will use symbols to represent parameters in quantum gates, allowing these parameters to be updated during optimization.\n", | ||
"2. **Circuit Execution with Binding**: We will bind parameter values to a circuit prior to its execution, a critical step in parameter optimization routines.\n", | ||
"3. **Simple Optimization Loop**: We'll implement a basic optimization loop that updates parameters based on a cost function.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Step 1: Setting Up\n", | ||
"\n", | ||
"We start by setting up the backend configuration and initializing the QuMat framework. This framework interfaces with quantum computing libraries like Qiskit or Cirq to manage the underlying quantum computations.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from qumat.qumat import QuMat\n", | ||
"\n", | ||
"# Configure the backend to use Qiskit with a simulator\n", | ||
"backend_config = {\n", | ||
" 'backend_name': 'qiskit',\n", | ||
" 'backend_options': {'simulator_type': 'qasm_simulator', 'shots': 1024}\n", | ||
"}\n", | ||
"\n", | ||
"# Create an instance of QuMat\n", | ||
"qumat_instance = QuMat(backend_config)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Step 2: Creating a Parameterized Quantum Circuit\n", | ||
"\n", | ||
"We create a simple quantum circuit with one qubit and apply parameterized RX, RY, and RZ gates. The parameters will be defined symbolically, allowing them to be replaced with actual values during execution.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create a quantum circuit with 1 qubit\n", | ||
"qumat_instance.create_empty_circuit(1)\n", | ||
"\n", | ||
"# Apply parameterized RX, RY, and RZ gates\n", | ||
"qumat_instance.apply_rx_gate(0, 'theta')\n", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a fair call out. They're standard in rotational gates, but it wouldn't hurt to add some docs calling them out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this runs for you I'm good approving; I'll make a new issue to add these def'ns later. |
||
"qumat_instance.apply_ry_gate(0, 'phi')\n", | ||
"qumat_instance.apply_rz_gate(0, 'lambda')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Step 3: Running the Optimization Loop\n", | ||
"\n", | ||
"We'll iterate over a simple optimization loop, updating the parameter values for each iteration. This example does not feature a sophisticated cost function, but in practical scenarios, you'd compute and leverage a cost function to guide these updates.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Example optimization loop\n", | ||
"\n", | ||
"# Initial parameter values\n", | ||
"current_parameters = {'theta': 0, 'phi': 0, 'lambda': 0}\n", | ||
"\n", | ||
"# Define a simple placeholder cost function\n", | ||
"def your_cost_function():\n", | ||
" # Placeholder: replace with a real function in actual applications\n", | ||
" return 0\n", | ||
"\n", | ||
"# Run the optimization loop\n", | ||
"for iteration in range(10): # Reduced iteration count for brevity\n", | ||
" cost = your_cost_function() # Evaluate the cost function\n", | ||
"\n", | ||
" # Update parameter(s) based on some optimization logic\n", | ||
" # This is a placeholder update mechanism\n", | ||
" current_parameters['theta'] += 0.1\n", | ||
" current_parameters['phi'] += 0.1\n", | ||
" current_parameters['lambda'] += 0.1\n", | ||
"\n", | ||
" # Execute the circuit with the updated parameters\n", | ||
" result = qumat_instance.execute_circuit(parameter_values=current_parameters)\n", | ||
"\n", | ||
" print(f\"Iteration {iteration}, Result: {result}, Parameters: {current_parameters}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Conclusion\n", | ||
"\n", | ||
"This notebook demonstrates how to effectively handle parameters within quantum circuits using the QuMat framework. Although the optimization loop here uses a placeholder mechanism, it highlights how parameterized circuits can be used in iterative optimization processes, often encountered in variational quantum algorithms.\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ | |
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"!pip install git+https://github.com/apache/mahout.git@main" | ||
"# pip install git+https://github.com/apache/mahout.git@main" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be commented out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for notebooks to actually be tested on the code that is being checked in it does. |
||
], | ||
"metadata": { | ||
"colab": { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need an "Open in Colab" button?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could, what is the standard on those? (I've seen in notebook, in a readme file, both, neither)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a rule of thumb but I know it's on the Simple Example and it's great. Let's just add to
.ipynb
files?