Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Language Concepts

In this workshop we will learn to use high-level quantum programming language concepts to design quantum algorithms. We will use the Qmod language to model the functionality, and the Classiq platform to synthesize it into gate-level descriptions, visualize the circuits, and execute them. We will focus on high-level quantum types and expressions in different evaluation modes. Part I is a walk through Qmod’s structure and constructs, as well as some of its unique high-level concepts. We will look closely at these constructs using small examples. In Part II we will combine some of these concepts in a complete quantum algorithm. There are 5 code exercises in this notebook, split into two sections, A and B. In each exercise follow the instructions - complete the code snippet where indicated by a TODO comment, execute the code, and try to understand the results. Solutions are provided at the end of the notebook. Don’t continue to the next exercise before you completed the previous one and compared your code and results against the solution.

Section A (15 Minutes)

Warmup: A First Qmod Program

Let’s start with a simple example to demonstrate the structure of Qmod code, as well as its synthesis and execution flow. We prepare and sample a Bell state - one of the most fundamental quantum states. Further reading: Quantum Functions
Quantum functions in Qmod are defined using a regular Python function, decorated with qfunc, and their parameters must be declared with type hints.
We can now compile with the SDK function synthesize. We get back an executable description called quantum program which we then execute on any simulation or quantum hardware. We then execute it — in our case, a simple sampling (using the default number of shots) of the quantum program will suffice.
Output:
Output:
Output:
Output:
You should see in approximately 50% of the samples the bit vector 00 and in 50% the bit vector
Synthesis is the process of compiling a high-level description to a gate-level description.The reduction is presented graphically.The executable format can be simulated using various simulation engines, or executed on quantum hardware of choice.

Exercise 1: GHZ State

Based on the Bell state example above, prepare a GHZ state with 3 qubits. The GHZ state creates maximum entanglement between three qubits: (000+111)/2(|000\rangle + |111\rangle)/\sqrt{2}.
You should see in approximately 50% of the samples the bit vector 000 and in 50% the bit vector

Exercise 2: GHZ with Numeric Variables

Define a main function that calls the create_ghz_state (as implemented in Exercise 1) with a signed quantum number and outputs the results. Synthesize it, analyze the quantum program, execute it and print the results. What do you expect the resulting value of x to be? Further reading: Quantum Types
Output:
Output:
Output:
Output:
You should see approximately 50% for 0 and 50% for -
In Qmod function arguments are automatically cast between QNum and QArray[QBit] (and also between other quantum types).The value of quantum variables is interpreted based on their type.The state 111|111\rangle represents 7 for an unsigned integer, and -1 as signed integer (in two’s complement encoding).

Section B (30 Minutes)

Exercise 3: Arithmetic Expressions with Automatic Type Inference

Part A: Numeric Type Inference

Create and execute a quantum program that assigns a quantum arithmetic expression to a numeric variable:
  1. Declare quantum numeric variables a and b as unsigned integers of size of
  2. Apply hadamard_transform on a and b (to put them in uniform superposition of all possible states).
  3. Assign the value of 3*a + b to c.
  4. Synthesize, show, execute and print results.
Inspect the printouts - what numeric attributes were inferred for variable c? Why? Further reading: Numeric Assignment
Output:
Output:
Output:
Output:
Output:
The expression result ranges between 0 and1
  1. To represent all values variable c must be an unsigned integer of size 4 qubits or more.
In Qmod the size of numeric variables may be left unspecified. It is then automatically inferred to tightly fit all possible values of the expressions.

Part B: Numeric Type Inference with Fixed-Point Fractions

Repeat Part A, only this time declaring a with 1 fraction digit and b with 2 fraction digits. How did the numeric attributes of c change? What are the corresponding sampled values for c in the result?
Output:
Output:
Output:
Output:
Output:
Variable c should be of size 5, unsigend, and with 2 fraction digits.This is the minimal type that covers the expression’s domain.
In Qmod, numeric variables can represent arbitrary fixed-point values.Arithemetic expression and type inference also accommodate for different decimal point locations.

Exercise 4: Conditional Operations

Define a main function that initializes a quantum variable x (a 3-qubit signed number with 2 binary fraction digits) in an equal superposition of all states, then conditionally flips a single-qubit variable named flag when the value of x is less than
  1. Inspect the execution results - how is flag entangled with x?
Further reading: Control Statement
Output:
Output:
Output:
Output:
You should see that x is evenly distributed across the 8 values, and flag is flipped in 6 out of the 8 cases.
The control statement in Qmod is similar to classical if statement, where a statement block is applied conditionally, depending on a Boolean expression, and optionally an else-block is applied otherwise.The difference is that the operations are applied in superposition, corresponding to the condition.

Exercise 5: Grover Algorithm Using Quantum Struct and Constant Phase

The Grover search algorithm uses two kinds of conditional phase flips - one reflecting about the “good” states, and the other reflecting about the zero state. This is easy to express using fixed π\pi phase rotation under the required control condition. It is also convenient to encapsulate the problem variables in a quantum struct, so they can be passed around between functional units. Create a quantum program that finds assignments for a,ba, b and c{0,1,2,3}c \in \{0, 1, 2, 3\} that satisfy the equation 3a+b+2c=93a + b + 2c = 9.
  • Declare the variables as fields of a quantum struct
  • Define the phase oracle in terms of the problem condition
  • Define the zero-reflection in the diffuser
Further reading: Phase Statement
Output:
Output:
Output:
Output:
There are 6 assignments to a, b, and c that satisfy the equation.They should be sampled with approximately equal probabilities.
In Qmod, a fixed-value phase (specifically π\pi) can be introduced under control condition.This can be used to apply a phase-flip to states described with a quantum expressions.

Exercise 6: Phase Arithmetic

Part A

Compute the expression xyx * y in the phase of their respective states. Use a coefficient to distribute all possible states over the 2π2\pi phase rotation. To actually view the phases of the states, simulate the program using state-vector simulation. Expand the quantum program visualization down to the gate-level implementation. How is the phase statement synthesized? Inspect the resulting phases of the different states in the printout. Do they match the phase expression over x and y? Further reading: Phase Statement
Output:
Output:
The resulting state phases should show x2x^2 rotation of the respective computational-state value, modulo 8 (determined by the domain of variable x).The steps are a 1/8 of a full 2π2\pi rotation
The phase statement can operate on an arbitrary polynomials over quantum numeric variables, introducing the corresponding Z rotations on the respective states.

Bonus: Fourier Arithmetic

Create a quantum program that computes y=x2y = x^2 by computing x2x^2 in the Fourier basis. Then transform the result back to the computational basis. Inspect the execution results to validate the correctness of your algorithm.
Output:
Output:
The phase statement can be used to implement modular arithmetic in the Fourier basis.

Solutions

Solution 1: GHZ State

Output:
Output:
Output:
Output:
Output:

Solution 2: GHZ with Quantum Numbers

Output:
Output:
Output:
Output:
Output:

Solution 3: Arithmetic Expressions with Automatic Type Inference

Part A: Numeric Type Inference

Output:
Output:
Output:
Output:
Output:
Output:

Part B: Numeric Type Inference with Fixed-Point Fractions

Output:
Output:
Output:
Output:
Output:
Output:

Solution 4: Conditional Operations

Output:
Output:
Output:
Output:
Output:

Solution 5: Grover Algorithm Using Quantum Struct and Constant Phase

Output:
Output:
Output:
Output:
Output:

Solution 6: Phase Arithmetic

Output:
Output:
Output:

Bonus

Output:
Output:
Output: