Let's solve a construction problem with Grover using Classiq

Create a large-scale Grover search algorithm using the Classiq platform. We will calculate the dimensions of a trapezoid, which has a specific volume, using the Grover algorithm.

Let's solve a construction problem with Grover using Classiq
Photo by Dan Cristian Pădureț / Unsplash

Let's see how we can use Classiq's synthesis engine and execution platform to calculate the dimensions of a shape.

Trapezoid volume

The question is as follows we want a 3D trapezoid, like the one pictured above, with a volume of 20. The question is, what are the dimensions that will give a trapezoid with this volume? As you can see, we have 4 variables that we have to pick.

The base area of a trapezoid

Let's first look at the base, like pictured above. To calculate the base area, we can use the following formula:

$$A = \frac{a+b}{2} \cdot h $$

Now if we want the total volume, we simply multiply the base area by the length, which gives the total volume function:

$$V = (\frac{a+b}{2} \cdot h) \cdot l $$

Because we want a volume of 20, we need to solve this function with a quantum computer:

$$20 = (\frac{a+b}{2} \cdot h) \cdot l $$

So let's create the quantum circuit. To make it very easy for us, I will use Classiq. This makes it easy to create the circuit and will ensure that the resulting circuits are efficient. The nice thing is that we do not need to write any gates or think about how to efficiently change our code for a specific machine. The only thing I want to be sure of is that no more than 32 qubits are used to make sure it can be run on the IBM QASM simulator.

To do all this, we need the arithmetic function we want to solve in a format that is accepted by the Classiq oracle. This is:

((0.5*(a+b))*h)*l == 20

Now we will solve this in the Classiq IDE with this qmod:

{
  "functions": [
    {
      "name": "main",
      "port_declarations": {
        "a": {
          "name": "a",
          "direction": "inout",
          "size": {
            "expr": "2"
          }
        },
        "b": {
          "name": "b",
          "direction": "inout",
          "size": {
            "expr": "3"
          }
        },
        "h": {
          "name": "h",
          "direction": "inout",
          "size": {
            "expr": "3"
          }
        },
        "l": {
          "name": "l",
          "direction": "inout",
          "size": {
            "expr": "3"
          }
        }
      },
      "body": [
        {
          "function": "GroverOperator",
          "function_params": {
            "oracle_params": {
              "expression": "((0.5*(a+b))*h)*l == 20",
              "definitions": {
                "a": {
                  "size": 2
                },
                "b": {
                  "size": 3
                },
                "h": {
                  "size": 3
                },
                "l": {
                  "size": 3
                }
              },
              "uncomputation_method": "optimized"
            }
          },
          "inputs": {
            "a": "a_in",
            "b": "b_in",
            "h": "h_in",
            "l": "l_in"
          },
          "outputs": {
            "a": "a_out",
            "b": "b_out",
            "h": "h_out",
            "l": "l_out"
          }
        }
      ]
    }
  ],
  "constraints": {
    "max_width": 32,
    "optimization_parameter": "depth"
  }
}

What does this piece of code do?

  1. Firstly, it declares the registers for the variables we will use. The size here is the number of qubits in the register.
  2. We create the Grover operator with the expression we want to solve.
  3. Lastly, we define the constraints, this makes sure we do not use more than 32 qubits and that the final circuit is optimized for depth.

After a few seconds of synthesis, we got this circuit:

This circuit has a depth of 2115 and uses 32 qubits.

Now we only need to do one last thing: execute this circuit to get the measurements of the potential trapezoid. We can use the free IBM QASM simulator because we limited the number of qubits to 32. This is the result we get:

Execution result

As you can see, this will give us a valid Trapezoid with a volume of 20.

Isn't this awesome how quickly you can create such complex quantum circuits whiteout having to dive into the gate level of a circuit. If there were simulators that were able to simulate larger circuits, we could execute even more complex circuits. These could, for example, have decimals or larger quantum registers.