0

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]);


            Adjoint Controlled refImpl([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 { private readonly ITestOutputHelper output;

    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);
        }
    }
}

}

3 Answers3

2

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.

Mariia Mykhailova
  • 9,325
  • 1
  • 13
  • 41
1

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$.

Martin Vesely
  • 15,398
  • 4
  • 32
  • 75
1

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$)