# Examples of Q# Programs

In the following, we review some Silq solutions to Microsoft’s Q# Summer 2018 and Winter 2019 coding contest on Codeforces. Detailed solutions targeted for Q# programmers are available for both 2018 and 2019.

## Generate Superposition of Zero State and a Basis State

Given classical bits $b \in 𝔹^n$ with $b[0]=1$, return state $\frac{1}{\sqrt{2}} \Big(\ket{b}+\ket{0} \Big)$, where $\ket{0}$ is represented using $n$ bits.

### Solution

The following solution in particular leverages

• vectors (𝔹^n),
• Hadamard transform (H)
• inline conditionals (if-then-else)
• type annotations (0:𝔹)
• type casts (as int[n] and as 𝔹^n), and
• forget

## Generate W State

Generate a generalized W state in $N=2^k$ qubits:

$W_N = \frac{1}{\sqrt{N}} \Big( \ket{100...0}+\ket{010...0} + ...+\ket{00...01}\Big)$

### Solution

The following solution in particular leverages

• Lambda-abstraction (λ...)

## Distinguish 3-qubit States

Determine if a given qubit is in state $\psi_0$ (return 0) or $\psi_1$ (return 1):

• $\psi_0=\frac{1}{\sqrt{3}} \Big( \ket{100} + \omega \ket{010} + \omega^2 \ket{001} \Big)$, where $\omega=e^{2i\pi/3}$
• $\psi_1=\frac{1}{\sqrt{3}} \Big( \ket{100} + \omega^2 \ket{010} + \omega \ket{001} \Big)$, where $\omega=e^{2i\pi/3}$

You may modify the given qubit arbitrarily (including measurements).

### Solution

Key Insights:

• $\psi_0 \perp \psi_1$: The two states are orthogonal.
• $\psi_0$ can be transformed to the W state on 3 qubits $W_3$. This process trasnforms $\psi_1$ to some state $\psi_1’$ orthogonal to $W_3$.
• We can generate $W_3$ from $\ket{000}$ (see Problem A4 above). Reversing this process generates $\ket{000}$ from $W_3$, and a state orthogonal to $\ket{000}$ from $\psi_1’$.

The following solution in particular leverages:

• decomposing lists: (head,)~tail:=qs
• reverse of a recursive function

## Check if String is Periodic

Implement a function solve with signature solve[n:!ℕ](x:𝔹^n):𝔹 and

$\text{solve}(\vec{x})=\begin{cases} 1 & \text{if } \vec{x} \text{ is periodic} \\\\ 0 & \text{otherwise} \end{cases}$

A bit string of length $n$ is considered periodic with period $P$ ($1 \leq P \leq n-1$) if for all $i \in {0,\dots, n-P-1}$, $x_i=x_{i+P}$.

### Solution

The following solution in particular leverages

• Automatic uncomputation of y and z when they are overwritten

## X-wing Fighter

Implement a unitary operation on $n$ qubits which is represented by a square matrix of size $2^n$ which has non-zero elements on both main diagonal and anti-diagonal and zero elements everywhere else.

For example, for $n=3$, the matrix of the operation should have the following shape:

X......X
.X....X.
..X..X..
...XX...
...XX...
..X..X..
.X....X.
X......X


Here, X denotes a “non-zero” element of the matrix, and . denotes a “zero” element of the matrix.

The row and column indices of the matrix follow little endian format: The least significant bit of the index is stored first in the qubit array. Thus, the second column of the matrix gives you the coefficients of the basis states you’ll get if you apply the unitary to the $\ket{10…0}$ basis state.

### Solution

Key insight: A possible solution is mapping $\ket{b_1 \cdots b_n}$ to $\frac{1}{\sqrt{2}}\ket{b_1 \cdots b_n} \pm \frac{1}{\sqrt{2}}\ket{\overline{b_1} \cdots \overline{b_n}}$, where $\overline{b_i}=1-b_i$. This is because the diagonal from top-left to bottom-right captures the identity, and the diagonal from bottom-left to top-right captures the mapping from $v$ to $N-v$ (where $N-v$ can be computed by inverting all bits of $v$).

The following solution in particular leverages

• swapping entries of a vector with other values