The Qmod language supports different classical types: scalars, arrays, and structs.Structs are objects with member variables or fields.See classical types.The built-in PauliTerm struct type is defined as follows:
Note that Pauli is an enum for all the Pauli matrices (I, X, Y, Z).A Pauli-based Hamiltonian can be represented as a list of PauliTerms. A Pauli operator defined this way is the argument to Hamiltonian evolution functions.This exercise uses the Suzuki-Trotter function to find the evolution of H=0.5XZXX + 0.25YIZI + 0.3 XIZY (captured as a literal value for the Pauli operator), with the evolution coefficient being 3, the order being 2, and using 4 repetitions.See suzuki_trotter.To complete this exercise, allocate q and invoke the suzuki_trotter quantum function:
This exercise uses quantum numeric variables and calculates expressions over them.See details on the syntax of numeric types in quantum types.See more on quantum expressions in numeric assignment.
The within-apply statement applies the U_dagger V U pattern that appears frequently in quantum computing.
It allows you to compute a function V within the context of another function U, and afterward uncompute U to release auxiliary qubits storing intermediate results.See within apply.
This exercise uses within-apply to compute an arithmetic expression in steps.Use the within_apply operation to calculate res = x + y + z from a two-variable addition building block with these steps:
Add x and y
Add the result to z
Uncompute the result of the first operation
For simplicity, initialize the registers to simple integers: x=3, y=5, z=2.Hints:
Use a temporary variable.
Use the function syntax of numeric assignment.
Execute the circuit and make sure you obtain the expected result.
Copy
Ask AI
from classiq import *# Your code here:qmod = create_model(main)qprog = synthesize(qmod)show(qprog)
Output:
Copy
Ask AI
Quantum program link: https://platform.classiq.io/circuit/356TIMg2TEqFRhPONwyHAoDQzMc
Why use within-apply and not just write three concatenated functions?
To understand the motivation, create another arithmetic circuit.This time, however, set the Classiq synthesis engine to optimize on the circuit’s number of qubits; i.e., its width.Determine constraints with the set_constraints operation. (See here).Perform the operation res = w + x + y + z, where w is initialized to 4 and the rest as before:
Add x and y (as part of the within_apply operation)
Add the result to z (as part of the within_apply operation)
Uncompute the result of the first operation (as part of the within_apply operation)
Add the result of the second operation to w.
There is no need to perform another uncomputation, as this brings the calculation to an end.Create the model, optimize on the circuit’s width, and run the circuit.Can you identify where qubits have been released and reused?
Copy
Ask AI
from classiq import *# Your code here:qmod = create_model(main)qprog = synthesize(qmod)show(qprog)
Output:
Copy
Ask AI
Quantum program link: https://platform.classiq.io/circuit/356TIUI9lRDQFWbK4igeclYheUA
What happens when you don’t manually decompose this expression?Use the Classiq arithmetic engine to calculate res |= x + y + z + w and optimize for width.Look at the resulting quantum program.Can you identify the computation and uncomputation blocks? What else do you notice?
Copy
Ask AI
from classiq import *# Your code here:qmod = create_model(main)qprog = synthesize(qmod)show(qprog)
Output:
Copy
Ask AI
Quantum program link: https://platform.classiq.io/circuit/356TIkwgB0CXvMqEhSsQ0A2ZDd0
This exercise uses quantum numeric variables that represent fixed-point reals.Arithmetic expressions can be calculated in place into a target variable, without allocating new qubits to store the result.This is done using the in-place-xor operator (^=) or the in-place-add operator (+=).See numeric assignment.In-place assignment is often used to nest arithmetic expressions under quantum operators.Note that out-of-place assignment requires its left-value variable to be uninitialized, and therefore cannot be used under an operator if the variable is declared outside its scope.Applying operators to arithmetic expressions is required in many algorithms.One example is the piecewise evaluation of mathematical functions; calculating different expressions over x depending on the subdomain where x falls.For this exercise, replace the missing parts in the code snippet below to evaluate the result:f(x)={2x+1x+0.5 if 0≤x<0.5 if 0.5≤x<1Note: in Python, assignment operators cannot be used in lambda expressions, so the computation of the function needs to be factored out to a named Python function.
Quantum program link: https://platform.classiq.io/circuit/356TJAjVWsqhvpxstdtv0f0x0Fu
The first bind operation splits the 3-qubit variable x into the 2-qubit and single-qubit lsb and msb variables, respectively.After the bind operation:
The lsb and msb variables can be operated on separately.
The x variable returns to its uninitialized state and can no longer be used.
The second bind operation concatenates the variables back to the res output variable.For this exercise, fill in the missing code parts in the above snippet and use the control statement to manually generate the 3-qubit probability distribution: [1/8, 1/8, 1/8 - sqrt(3)/16, 1/8 + sqrt(3)/16, 1/8, 1/8, 1/8, 1/8].The following sequence of operations generates it:
Perform the Hadamard transform on all three qubits.
Apply a pi/3 rotation on the LSB conditioned by the MSB being ∣0⟩ and the second-to-last MSB being ∣1⟩.
How would you write this condition using a QNum?To validate your results without looking at the full solution, compare them to running using the Classiq built-in prepare_state function.
# Solution to Exercise 7a:from classiq import *@qfuncdef main(res: Output[QNum]) -> None: x = QNum("x") y = QNum("y") z = QNum("z") x |= 2 y |= 7 z |= 1 res |= x + y # res |= x * y # res |= x * y - zqmod = create_model(main)qprog = synthesize(qmod)show(qprog)
Output:
Copy
Ask AI
Quantum program link: https://platform.classiq.io/circuit/356TJb3RPdAcdP9eAdaa0Ol1Km7
Copy
Ask AI
# Solution to Exercise 7b:from classiq import *@qfuncdef main(res: Output[QNum]) -> None: x = QNum("x") y = QNum("y") prepare_state([0.5, 0, 0.5, 0.0], 0.0, x) prepare_state([0, 0.25, 0.25, 0.25, 0.0, 0.0, 0.25, 0.0], 0.0, y) res |= x + y drop(x) # X and y cannot be uncomputed automatically so we leave them as trash. drop(y)qmod = create_model(main)qprog = synthesize(qmod)show(qprog)
Output:
Copy
Ask AI
Quantum program link: https://platform.classiq.io/circuit/356TK0ghiB5vjC4UTwYbLOsFt2h