Skip to content

Commit

Permalink
Global phase (cda-tum#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
pehamTom authored Aug 12, 2022
1 parent 8f1d73b commit 60a9f60
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion extern/json
Submodule json updated 398 files
2 changes: 1 addition & 1 deletion extern/zx
12 changes: 12 additions & 0 deletions src/zx/FunctionalityConstruction.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "zx/FunctionalityConstruction.hpp"

#include "Definitions.hpp"
#include "Rational.hpp"
#include "ZXDiagram.hpp"

#include <algorithm>
Expand Down Expand Up @@ -115,6 +116,11 @@ namespace zx {
break;

case qc::OpType::RZ:
diag.addGlobalPhase(-PiRational(op->getParameter().front()) / 2);
addZSpider(
diag, target, qubits,
Expression(PiRational(op->getParameter().front())));
break;
case qc::OpType::Phase:
addZSpider(
diag, target, qubits,
Expand All @@ -126,19 +132,25 @@ namespace zx {
break;

case qc::OpType::RX:
diag.addGlobalPhase(-PiRational(op->getParameter().front()) / 2);
addXSpider(
diag, target, qubits,
Expression(PiRational(op->getParameter().front())));
break;

case qc::OpType::Y:
diag.addGlobalPhase(-PiRational(1, 2));

addZSpider(diag, target, qubits,
Expression(PiRational(1, 1)));
addXSpider(diag, target, qubits,
Expression(PiRational(1, 1)));
break;

case qc::OpType::RY:
diag.addGlobalPhase(-PiRational(op->getParameter().front()) / 2 +
PiRational(1, 2) + PiRational(3, 2));

addXSpider(diag, target, qubits,
Expression(PiRational(1, 2)));
addZSpider(diag, target, qubits,
Expand Down
23 changes: 22 additions & 1 deletion test/unittests/test_zx_functionality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "zx/FunctionalityConstruction.hpp"

#include "gtest/gtest.h"
#include <cstddef>
#include <iostream>
#include <sstream>

Expand Down Expand Up @@ -120,7 +121,9 @@ TEST_F(ZXDiagramTest, complex_circuit) {
zx::fullReduce(diag);
EXPECT_EQ(diag.getNVertices(), 6);
EXPECT_EQ(diag.getNEdges(), 3);
EXPECT_TRUE(diag.isIdentity());
EXPECT_TRUE(diag.connected(diag.getInput(0), diag.getOutput(0)));
EXPECT_TRUE(diag.connected(diag.getInput(1), diag.getOutput(1)));
EXPECT_TRUE(diag.connected(diag.getInput(2), diag.getOutput(2)));
}

TEST_F(ZXDiagramTest, Phase) {
Expand Down Expand Up @@ -201,3 +204,21 @@ TEST_F(ZXDiagramTest, InitialLayout) {
zx::fullReduce(d);
EXPECT_TRUE(d.isIdentity());
}

TEST_F(ZXDiagramTest, RZ) {
qc = qc::QuantumComputation(1);
qc.rz(0, zx::PI / 8);

auto qcPrime = qc::QuantumComputation(1);
qcPrime.phase(0, zx::PI / 8);

auto d = zx::FunctionalityConstruction::buildFunctionality(&qc);
auto dPrime = zx::FunctionalityConstruction::buildFunctionality(&qcPrime);

d.concat(dPrime.invert());

zx::fullReduce(d);
EXPECT_FALSE(d.isIdentity());
EXPECT_FALSE(d.globalPhaseIsZero());
EXPECT_TRUE(d.connected(d.getInput(0), d.getOutput(0)));
}

0 comments on commit 60a9f60

Please sign in to comment.