Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ scratch/
.history
.vscode
*.png
dev.jl
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## underdevelop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "underdeveloped" mean? Unfinished or unreleased?

If it is "unfinished", let's just make sure we finish them in a well structured PR. If it is "unreleased", then put them with the rest in v0.1.0-dev which is also unreleased.

- Functions to check commutation between 2 Circuit Operations
- Corresponding Tests for commutation checks

## v0.1.0 - dev

- Basic datastructures for representing circuits through ADT with Moshi.jl
Expand Down
84 changes: 84 additions & 0 deletions src/MainIteration.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
This file contains the main iteration structures and functions for the PBCCompiler.
It defines how to propagate Pauli Product Measurements (PPMs) through a quantum circuit
Provides functions to handle commute condition and anticommute condition
Provides functions to handle PBC List update and Check list update
"""


using PBCCompiler
using PBCCompiler: Circuit, CircuitOp, Measurement, ExpHalfPiPauli, ExpQuatPiPauli, ExpEighPiPauli, PauliConditional, BitConditional, affectedqubits
using QuantumClifford: comm, embed
using Moshi.Match: @match


function affectedpaulis(op::CircuitOp.Type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring to the function that explains what it is and what it is for. Use jldoctest to put in examples of how to use it.

qubits = @match op begin
CircuitOp.Pauli(pauli, qubits) => pauli
CircuitOp.Measurement(pauli, bit, qubits) => pauli
CircuitOp.ExpHalfPiPauli(pauli, qubits) => pauli
CircuitOp.ExpQuatPiPauli(pauli, qubits) => pauli
CircuitOp.ExpEighPiPauli(pauli, qubits) => pauli
CircuitOp.PauliConditional(cp, cq, tp, tq) => vcat(cp, tp)
end
end

function complete_paulis(op1::CircuitOp.Type, op2::CircuitOp.Type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring, as mentioned for the previous one. Check the julia documentation on how to structure docstrings.

pu1=affectedpaulis(op1)
pu2=affectedpaulis(op2)
println("Affected Paulis of op1: ", pu1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not have your functions uncontrollably print things out -- that would make the library very very noisy and difficult to use without polluting the stdout. If these messages are valuable for you as you are working on the library, use @debug logging statements -- they can be controlled more easily and by default are quiet.

println("Affected Paulis of op2: ", pu2)
qu1=affectedqubits(op1)
qu2=affectedqubits(op2)
println("Affected qubits of op1: ", qu1)
println("Affected qubits of op2: ", qu2)
AffectedQubbits=sort(union(qu1,qu2))
println("Affected qubits of both ops: ", AffectedQubbits)
Paulilen=maximum(AffectedQubbits)
println("Length of the affected Pauli string: ", Paulilen)
Pauli1=embed(Paulilen, qu1, pu1)
Pauli2=embed(Paulilen, qu2, pu2)
println("New Pauli string of op1: ", Pauli1)
println("New Pauli string of op2: ", Pauli2)
return (Pauli1, Pauli2)
end

function check_commutation(op1::CircuitOp.Type, op2::CircuitOp.Type)

@match (op1, op2) begin
#scenario 1: One of them is classical controlled gate
(op,CircuitOp.BitConditional(inner_op, bit)) || (CircuitOp.BitConditional(inner_op, bit), op) => begin
println("Invalid input: Need to determine gate present first")
end
#scenario 2: One of them is Pauli Conditional gate
(op, CircuitOp.PauliConditional(cp, cq, tp, tq)) || (CircuitOp.PauliConditional(cp, cq, tp, tq), op) => begin
println("One of the operations is a Pauli conditional gate. ")
cop=ExpQuatPiPauli(cp, cq)
top=ExpQuatPiPauli(tp, tq)
comm_cop=check_commutation(op, cop)
comm_top=check_commutation(op, top)
if comm_cop == 0 && comm_top == 0
println("The operation commutes with both the control and target Paulis of the conditional gate.")
elseif comm_cop != 0 && comm_top != 0
println("The operation anticommutes with both the control and target Paulis of the conditional gate.")
else
println("The operation commutes with one of the control and target Paulis of the conditional gate, and anticommutes with the other.")
end
return (comm_cop, comm_top)
end
#scenario 3: Both are Pauli Product Rotations
_ => begin
(Pauli1,Pauli2)=complete_paulis(op1, op2)
commutativity=comm(Pauli1,Pauli2)
if commutativity == 0
println("The two operations commute.")
else
println("The two operations anticommute.")
end
return commutativity
end
#scenario 2: one is a Controlled gate

end
end

2 changes: 1 addition & 1 deletion src/PBCCompiler.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module PBCCompiler

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not edit unrelated parts of the files, it makes the diffs noisy and more difficult to review

using Moshi.Data: @data
using Moshi.Match: @match
using QuantumClifford: PauliOperator, @P_str
Expand Down Expand Up @@ -64,6 +63,7 @@ using .CircuitOp: Measurement, Pauli, ExpHalfPiPauli, ExpQuatPiPauli, ExpEighPiP
include("traversal.jl")
include("affectedqubits.jl")
include("plotting.jl")
include("MainIteration.jl")

##

Expand Down
87 changes: 87 additions & 0 deletions test/test_commutation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@testitem "check_commutation" tags=[:check_commutation] begin

using PBCCompiler
using PBCCompiler: Circuit, CircuitOp, Measurement, ExpHalfPiPauli, ExpQuatPiPauli, ExpEighPiPauli, PauliConditional, BitConditional, affectedqubits
using Test
using PBCCompiler: check_commutation
using QuantumClifford: @P_str, comm
@testset "Test unordered input" begin
op1=ExpQuatPiPauli(P"XY", [1, 3])
op2=ExpQuatPiPauli(P"ZXY", [3, 1, 2])

t_1=comm(op1.pauli,op2.pauli)
@test t_1 == 0x00

t_2=check_commutation(op1, op2)
@test t_2 == 0x01

end

@testset "Test non-overlapping input" begin
op1=ExpQuatPiPauli(P"X", [1])
op2=ExpQuatPiPauli(P"Z", [2])

t_1=comm(op1.pauli,op2.pauli)
@test t_1 == 0x01

t_2=check_commutation(op1, op2)
@test t_2 == 0x00

end

@testset "Test partially overlapping input" begin
op1=ExpQuatPiPauli(P"XX", [1, 3])
op2=ExpQuatPiPauli(P"ZY", [3, 4])

t_1=comm(op1.pauli,op2.pauli)
@test t_1 == 0x00

t_2=check_commutation(op1, op2)
@test t_2 == 0x01

end

@testset "Test different Pauli Product Rotation input" begin
op1=ExpHalfPiPauli(P"X", [1])
op2=ExpQuatPiPauli(P"Z", [1])
op3=ExpEighPiPauli(P"Y", [1])

t_1=check_commutation(op1,op2)
@test t_1 == 0x01

t_2=check_commutation(op1, op2)
@test t_2 == 0x01

t_3=check_commutation(op1,op3)
@test t_3 == 0x01

end

@testset "Test Pauli Conditional input" begin
op1=ExpQuatPiPauli(P"ZX", [1, 2])
op2=ExpQuatPiPauli(P"ZY", [1, 2])
op3=ExpQuatPiPauli(P"YX", [1, 2])
CNOT=PauliConditional(P"Z", [1], P"X", [2])

t_1=check_commutation(op1,CNOT)
@test t_1 == (0x00,0x00)

t_2=check_commutation(op2,CNOT)
@test t_2 == (0x00,0x01)

t_3=check_commutation(op3,CNOT)
@test t_3 == (0x01,0x00)

end

@testset "Test Bit Conditional input" begin
op1=ExpQuatPiPauli(P"X", [1])
op2=ExpQuatPiPauli(P"Z", [1])
bit_cond_op=BitConditional(op1, 0)

t_1=check_commutation(bit_cond_op, op2)
@test t_1 === nothing

end

end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice the warning about a missing new line at the end of this file. Set your editor to clean these things automatically. Here is the settings I have added to vscode for instance:

        "editor.detectIndentation": false,
        "editor.insertSpaces": true,
        "editor.tabSize": 4,
        "files.insertFinalNewline": true,
        "files.trimFinalNewlines": true,
        "files.trimTrailingWhitespace": true,
        "editor.rulers": [92],
        "files.eol": "\n",

Some of these might be redundant -- they have grown organically.

Loading