From 60a9f604fdfe8cca5824276307979c1bc8fc1c9d Mon Sep 17 00:00:00 2001 From: Tom Peham Date: Fri, 12 Aug 2022 20:50:04 +0200 Subject: [PATCH] Global phase (#166) --- extern/json | 2 +- extern/zx | 2 +- src/zx/FunctionalityConstruction.cpp | 12 ++++++++++++ test/unittests/test_zx_functionality.cpp | 23 ++++++++++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/extern/json b/extern/json index a92ccafd..954b10ad 160000 --- a/extern/json +++ b/extern/json @@ -1 +1 @@ -Subproject commit a92ccafd2c7709243451e947a21ad08203273483 +Subproject commit 954b10ad3baa5d92bb9cd5bb93c7258433cd2bb2 diff --git a/extern/zx b/extern/zx index 41cb6f03..86429cb9 160000 --- a/extern/zx +++ b/extern/zx @@ -1 +1 @@ -Subproject commit 41cb6f03ffcc933445bbc3acdb543d666050d635 +Subproject commit 86429cb98036e6a2a754b5dce849d0903145d56c diff --git a/src/zx/FunctionalityConstruction.cpp b/src/zx/FunctionalityConstruction.cpp index c026eb7e..3e3e16bb 100644 --- a/src/zx/FunctionalityConstruction.cpp +++ b/src/zx/FunctionalityConstruction.cpp @@ -1,6 +1,7 @@ #include "zx/FunctionalityConstruction.hpp" #include "Definitions.hpp" +#include "Rational.hpp" #include "ZXDiagram.hpp" #include @@ -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, @@ -126,12 +132,15 @@ 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, @@ -139,6 +148,9 @@ namespace zx { 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, diff --git a/test/unittests/test_zx_functionality.cpp b/test/unittests/test_zx_functionality.cpp index 816c6e54..12ae31a0 100644 --- a/test/unittests/test_zx_functionality.cpp +++ b/test/unittests/test_zx_functionality.cpp @@ -11,6 +11,7 @@ #include "zx/FunctionalityConstruction.hpp" #include "gtest/gtest.h" +#include #include #include @@ -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) { @@ -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))); +}