# Quantum Katas - Tutorials - SingleQubitGates - Exercise 2 - GlobalPhaseI

Program.qs

namespace Quantum.Kata.SingleQubitGates {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;

operation GlobalPhaseI (q : Qubit) : Unit is Adj+Ctl {
X(q);
Z(q);
Y(q);

}
}


Reference.qs

namespace Quantum.Kata.SingleQubitGates {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Math;

operation GlobalPhaseI_Reference (q : Qubit) : Unit is Adj+Ctl {
X(q);
Z(q);
Y(q);
}
}


Test.qs

    open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;

operation ControlledArrayWrapperOperation (op : (Qubit => Unit is Adj+Ctl), qs : Qubit[]) : Unit is Adj+Ctl {
Controlled op([qs[0]], qs[1]);
}

operation AssertEqualOnZeroState (testImpl : (Qubit => Unit is Ctl), refImpl : (Qubit => Unit is Adj+Ctl)) : Unit {
using (qs = Qubit[2]) {
within {
H(qs[0]);
}
apply {

Controlled testImpl([qs[0]], qs[1]);

}

AssertAllZero(qs);
}
}

operation T2_GlobalPhaseI_Test () : Unit {
AssertOperationsEqualReferenced(2, ControlledArrayWrapperOperation(GlobalPhaseI, _), ControlledArrayWrapperOperation(GlobalPhaseI_Reference, _));
}



Driver.cs


using Microsoft.Quantum.Simulation.XUnit;
using Microsoft.Quantum.Simulation.Simulators;

using Xunit.Abstractions;

namespace Quantum.Kata.SingleQubitGates
{
public class TestSuiteRunner
{

public TestSuiteRunner(ITestOutputHelper output)
{
this.output = output;
}

/// <summary>

/// </summary>
[OperationDriver(TestNamespace = "Quantum.Kata.SingleQubitGates")]
public void TestTarget(TestOperation op)
{
using (var sim = new QuantumSimulator())
{

sim.OnLog += (msg) => { output.WriteLine(msg); };
sim.OnLog += (msg) => { Debug.WriteLine(msg); };
op.TestOperationRunner(sim);
}
}
}
}



Yes, this sequence of gates will work to apply the global phase of $$i$$. You can check it using matrix multiplication - a product of matrices $$Y \cdot Z \cdot X$$ will give you a matrix $$\begin{bmatrix} i & 0 \\ 0 & i \end{bmatrix}$$, which corresponds to applying a global phase.

Alternatively, you can implement it using R gate with the phase $$-\pi$$: R(PauliI, -PI(), q);

As a side note, performing measurements this way is not going to detect a global phase introduced by the gate; you need to work with a controlled version of the gate to detect it.

• I tried to work with a controlled version of the gate but I could not add a Entry Potint and run it. How can I run the code with a controlled version ? – theRomanMercury Jun 22 at 19:29
• EntryPoint and controlled version of the gate are completely unrelated. What exactly didn't work for you? You just need to allocate two qubits, prepare the first one in the |+⟩ state and apply controlled gate with the first qubit as control and second as target. – Mariia Mykhailova Jun 23 at 5:38
• I updated my question with your answer. I used this codes from Katas but I did not get an output when I run it. Is it because they are Unit type? – theRomanMercury Jun 23 at 9:03
• Nothing in the code you've pasted writes anything to the output. You can use Message to output classical values and DumpMachine to output internal state of the quantum simulator. – Mariia Mykhailova Jun 23 at 23:10

On IBM Q, you can also use $$U3$$ gate to prepare a global phase operator. $$U3$$ gate is defined as $$U3(\theta, \varphi, \lambda) = \begin{pmatrix} \cos (\theta/2) & -\mathrm{e}^{i\lambda}\sin(\theta/2) \\ \mathrm{e}^{i\varphi}\sin(\theta/2) & \mathrm{e}^{i(\varphi + \lambda)}\cos(\theta/2)\\ \end{pmatrix}.$$ Setting $$\theta = \pi$$ we get $$U3(\pi, \varphi, \lambda) = \begin{pmatrix} 0 & -\mathrm{e}^{i\lambda} \\ \mathrm{e}^{i\varphi} & 0\\ \end{pmatrix}.$$ Let's denote our global phase $$\alpha$$ and set $$\varphi = \alpha$$ and $$\lambda = \alpha + \pi$$. Since $$-\mathrm{e}^{i\pi}=1$$ we have $$U3(\pi, \alpha, \alpha+\pi) = \begin{pmatrix} 0 & \mathrm{e}^{i\alpha} \\ \mathrm{e}^{i\alpha} & 0\\ \end{pmatrix},$$ which is $$\mathrm{e^{i\alpha}}X$$. To get $$\mathrm{e^{i\alpha}}I$$, we apply another $$X$$ gate.

So, global phase gate is implemented as $$X\,\,U3(\pi,\alpha,\alpha+\pi)$$, where $$\alpha$$ is global phase.

EDIT (solution in Q#)

A $$R1$$ gate is defined as $$R1(\theta) = \begin{pmatrix} 1 & 0 \\ 0 & \mathrm{e}^{i\theta} \end{pmatrix}$$

An operation $$X\,R1(\theta)$$ is described by matrix $$\begin{pmatrix} 0 & \mathrm{e}^{i\theta} \\ 1 & 0 \end{pmatrix}$$

If we apply this operation twice (i.e. $$[X\,R1(\theta)]^2$$ ), we get

$$\begin{pmatrix} \mathrm{e}^{i\theta} & 0 \\ 0 & \mathrm{e}^{i\theta} \end{pmatrix},$$

which is a global phase gate with arbitrary phase $$\theta$$.

So, global phase gate in Q# can be realized as $$[X\,R1(\theta)]^2$$.

• I am focusing on Q# language, thanx a lot for your answer. – theRomanMercury Jun 23 at 9:04
• @theRomanMercury: Sorry, I did not take this into account. Please find solution (global phase gate with arbitrary phase) with $R1$ and $X$ gates for Q#. – Martin Vesely Jun 24 at 7:34

I apologize, but I can not comment on @Martin Vesely answer above. I am glad that my amendments have already been adopted, I’ll leave only a short note, in case someone is interested in this: In Qiskit programs (e.g. see) I use X before U3 in my variants of phase shift circuits, in an attempt to avoid "distortion" during further processing of Qiskit (for example, by transpiler):

qc.x(qubit)
qc.u3(np.pi, gamma, np.pi + gamma, qubit)


If not for similar considerations, then the order here would not be important (as in other similar cases, e.g. $$[X\,R1(\theta)]^2 = [R1(\theta)\,X]^2$$)