View on GitHub
Open this notebook in GitHub to run it yourself
Learning Optimization
This is a step-by-step example of how to use the Classiq platform at the application level. The goal is to see how easy it can be to use quantum algorithms to solve problems. This is a basic optimization problem: minimize the expression for the non-negative integers , given the constraint that . Of course this is trivial and the solution is . The goal is to understand how to incorporate the problem with the platform, so you can then continue on to define more complicated problems. This kind of optimization problem is relevant for many real-life scenarios. For example, Amazon wants to determine the best value it can offer to a customer for two items on a Black Friday sale, but it has to earn some minimum value.How to Solve It?
Define the optimization problem with the classical Pyomo optimization package in Python. Then, use the platform to convert it to a high-level functional model of quantum algorithms. This functional model is at the heart of the platform as this is the object that is synthesized to an actual quantum circuit using the synthesis engine! After the circuit is synthesized, run it on actual hardware or on a simulator to actually get the result from the quantum algorithm. This tutorial runs the algorithm on the IBM quantum simulator as it is the default option.What Do You Need to Know about Quantum Algorithms?
You need to know almost nothing regarding quantum algorithms, besides one thing. There are two common algorithms used for optimization problems (as well as chemistry): QAOA and VQE. Both are very similar, where QAOA could be seen as a specific type of VQE. For this problem, use the QAOA algorithm. The algorithm has a mandatory parameter that you need to choose, as explained below.Getting Started
Import the relevant packages. The first is the Pyomo package; the classical optimization package that was installed when you installed Classiq:Defining the Problem
Initiate a Pyomo application object.pyo.Var object:
- The names of the variables.
- The type/domain of the variables.
- The bounds of the variables.
constraint, which is equal to some Pyomo constraint object.
Read link for more ways of defining constraints.
Examine the application object using the Pyomo method ‘pprint’:
Output:
Entering the Quantum World
So far, you have only used the Pyomo package and spoken the optimization language. You now need some quantum knowledge for the optimization problem. Set the number of repetitions of the QAOA sub-circuit. The QAOA algorithm contains a QAOA sub-circuit that might repeat several times. Roughly speaking, the more repetitions, the better the algorithm. Having said that, as you saw, there is a limited range of parameters due to the small size of today’s quantum computers. There is also an issue with the length of the quantum circuit due to the relatively low quality of today’s quantum circuits (again due to the limited power of the quantum simulators). Therefore, start with one repetition of the sub-circuit, and later you can change it to see how the results change. Define the QAOA configuration:Seamlessly Generating the Functional Level Model
Now with the application object and the QAOA configuration defined, ask the platform to convert it into a high-level quantum functional model.| solution | probability | cost | |
|---|---|---|---|
| 211 | {‘x’: [1, 0], ‘constraint_slack_var’: [1, 0, 0… | 0.002441 | 3.0 |
| 35 | {‘x’: [0, 2], ‘constraint_slack_var’: [0, 0, 0… | 0.005371 | 4.0 |
| 241 | {‘x’: [1, 1], ‘constraint_slack_var’: [0, 1, 0… | 0.001465 | 5.0 |
| 225 | {‘x’: [2, 0], ‘constraint_slack_var’: [1, 0, 0… | 0.002441 | 6.0 |
| 103 | {‘x’: [2, 0], ‘constraint_slack_var’: [0, 0, 1… | 0.003906 | 6.0 |
Output:
Output: