The "Unitary Patterns" quantum kata is a series of exercises designed to get you comfortable with creating unitary transformations which can be represented with matrices of certain shapes (with certain pattern of zero and non-zero values).
Each task is wrapped in one operation preceded by the description of the task.
Your goal is to fill in the blank (marked with the // ...
comments)
with some Q# code that solves the task. To verify your answer, run the cell using Ctrl+Enter (⌘+Enter on macOS).
Within each section, tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks.
Each task describes a matrix which your unitary needs to implement.
The row and column indices of the matrix elements are in little-endian format (the least significant bit is stored first).
For example, index 1 corresponds to the qubit state $|10...0\rangle$, and to store this state in an array of qubits qs
its first element qs[0]
would have to be in state $|1\rangle$ and the rest of the qubits would have to be in state $|0\rangle$.
In the example patterns provided in the tasks, X
marks a "non-zero" element, and .
marks a "zero" element.
A "zero" element of a matrix is a complex number which has an absolute value of 0.001 or less,
and a "non-zero" element is a complex number which has an absolute value of 0.001 or greater.
You can see the details of the verification in Tests.qs
file, operation AssertOperationMatrixMatchesPattern
.
Note that all tasks require you to implement a unitary transformation, which means that you're not allowed to use any measurements.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements on the main diagonal and zero elements everywhere else.
Example: For N = 2, the matrix of the transformation should look as follows:
X...
.X..
..X.
...X
%kata T01_MainDiagonal
operation MainDiagonal (qs : Qubit[]) : Unit {
// The simplest example of such a unitary transformation is represented by an identity matrix.
// This means that the operation doesn't need to do anything with the input qubits.
// Execute this cell to see that this solution is correct.
// You are welcome to try and come up with other diagonal unitaries.
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with all elements non-zero.
Example: For N = 2, the matrix of the transformation should look as follows:
XXXX
XXXX
XXXX
XXXX
%kata T02_AllNonZero
operation AllNonZero (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix which has $2 \times 2$ blocks of non-zero elements on the main diagonal and zero elements everywhere else.
Example: For N = 3, the matrix of the transformation should look as follows:
XX......
XX......
..XX....
..XX....
....XX..
....XX..
......XX
......XX
%kata T03_BlockDiagonal
operation BlockDiagonal (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements in top left and bottom right quarters and zero elements everywhere else.
Example: For N = 3, the matrix of the transformation should look as follows:
XXXX....
XXXX....
XXXX....
XXXX....
....XXXX
....XXXX
....XXXX
....XXXX
%kata T04_Quarters
operation Quarters (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements in positions where row and column indices have the same parity and zero elements everywhere else.
Example: For N = 2, the matrix of the transformation should look as follows:
X.X.
.X.X
X.X.
.X.X
%kata T05_EvenChessPattern
operation EvenChessPattern (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements in positions where row and column indices have different parity and zero elements everywhere else.
Example: For N = 2, the matrix of the transformation should look as follows:
.X.X
X.X.
.X.X
X.X.
%kata T06_OddChessPattern
operation OddChessPattern (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements on the anti-diagonal and zero elements everywhere else.
Example: For N = 2, the matrix of the transformation should look as follows:
...X
..X.
.X..
X...
%kata T07_Antidiagonal
operation Antidiagonal (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix in which zero and non-zero elements form a chessboard pattern with 2x2 squares, with the top left square occupied by non-zero elements.
Example: For N = 3, the matrix of the transformation should look as follows:
XX..XX..
XX..XX..
..XX..XX
..XX..XX
XX..XX..
XX..XX..
..XX..XX
..XX..XX
%kata T08_ChessPattern2x2
operation ChessPattern2x2 (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with
Example: For N = 2, the matrix of the transformation should look as follows:
.X..
X...
..XX
..XX
%kata T09_TwoPatterns
operation TwoPatterns (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix defined recursively:
Example: For N = 3, the matrix of the transformation should look as follows:
X.......
.X......
..XX....
..XX....
....XXXX
....XXXX
....XXXX
....XXXX
%kata T10_IncreasingBlocks
operation IncreasingBlocks (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements on the main diagonal and the anti-diagonal and zero elements everywhere else.
Example: For N = 3, the matrix of the transformation should look as follows:
X......X
.X....X.
..X..X..
...XX...
...XX...
..X..X..
.X....X.
X......X
%kata T11_XWing_Fighter
operation XWing_Fighter (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state.
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements forming a rhombus of width 1 with sides parallel to main diagonals.
Example: For N = 2, the matrix of the transformation should look as follows:
.XX.
X..X
X..X
.XX.
For N = 3, the matrix of the transformation should look as follows:
...XX...
..X..X..
.X....X.
X......X
X......X
.X....X.
..X..X..
...XX...
%kata T12_Rhombus
operation Rhombus (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the Unitary Patterns Workbook.
Input: N qubits in an arbitrary state ($2 \leq N \leq 5$).
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements in the following positions:
The central $2 \times 2$ sub-matrix;
The diagonals of the top right and bottom left sub-matrices of size $2^{N-1}-1$
that do not overlap with the central $2 \times 2$ sub-matrix;
that do not overlap with the central $2 \times 2$ sub-matrix.
Example:
For N = 3, the matrix of the transformation should look as follows:
..X..X..
.X....X.
X......X
...XX...
...XX...
X......X
.X....X.
..X..X..
%kata T13_TIE_Fighter
operation TIE_Fighter (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the [Unitary Patterns Workbook](./Workbook_UnitaryPatterns.ipynb#Task-13.-TIE-fighter).
Input: 3 qubits in an arbitrary state.
Goal: Implement a unitary transformation on 3 qubits which is represented by a matrix with non-zero elements in the following pattern:
XX....XX
XX....XX
...XX...
...XX...
..X..X..
..X..X..
XX....XX
XX....XX
%kata T14_Creeper
operation Creeper (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the [Unitary Patterns Workbook](./Workbook_UnitaryPatterns.ipynb#Task-14.-Creeper).
Input: N qubits in an arbitrary state ($2 \leq N \leq 4$).
Goal: Implement a unitary transformation on N qubits which is represented by a matrix with non-zero elements forming an upper diagonal matrix plus the first subdiagonal. This is called a Hessenberg matrix.
Example: For N = 2, the matrix of the transformation should look as follows:
XXXX
XXXX
.XXX
..XX
For N = 3, the matrix of the transformation should look as follows:
XXXXXXXX
XXXXXXXX
.XXXXXXX
..XXXXXX
...XXXXX
....XXXX
.....XXX
......XX
%kata T15_Hessenberg_Matrix
operation Hessenberg_Matrix (qs : Qubit[]) : Unit {
// ...
}
Can't come up with a solution? See the explained solution in the [Unitary Patterns Workbook](./Workbook_UnitaryPatterns.ipynb#Task-15.-Hessenberg-matrices).