Skip to content

Commit

Permalink
Fix Yao extension by properly transposing matrices (#212)
Browse files Browse the repository at this point in the history
* Transpose the matrix from Yao Blocks

* Enhance Yao extension tests

* Format code from tests

* Add two-qubit gate test

* Change permutation of indices instead of array

* Format tests
  • Loading branch information
jofrevalles committed Oct 8, 2024
1 parent 8191c96 commit afb85bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/TenetYaoExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Tenet.Quantum(circuit::AbstractBlock)
map(occupied_locs(gate)) do l
from, to = last(wire[l]), Tenet.nextindex!(gen)
push!(wire[l], to)
(from, to)
(to, from)
end,
)

Expand Down
42 changes: 42 additions & 0 deletions test/integration/YaoBlocks_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@testset "YaoBlocks" begin
using YaoBlocks

# NOTE qubit #3 left empty on purpose
circuit = chain(3, put(1 => X), cnot(1, 2))
tn = Quantum(circuit)

@test issetequal(sites(tn), [site"1", site"2", site"1'", site"2'"])
@test Tenet.ntensors(tn) == 2

@testset "GHZ Circuit" begin
circuit_GHZ = chain(n_qubits, put(1 => Yao.H), Yao.control(1, 2 => Yao.X), Yao.control(2, 3 => Yao.X))

quantum_circuit = Quantum(circuit_GHZ)

zeros = Quantum(Product(fill([1, 0], n_qubits))) #|000>
ones = Quantum(Product(fill([0, 1], n_qubits))) #|111>

expected_value = Tenet.contract(merge(zeros, quantum_circuit, ones')) # <111|circuit|000>
@test only(expected_value) 1 / 2

SV_Yao = apply!(zero_state(n_qubits), circuit_GHZ) # circuit|000>
@test only(statevec(ArrayReg(bit"111"))' * statevec(SV_Yao)) 1 / 2
end

@testset "two-qubit gate" begin
U = matblock(rand(ComplexF64, 4, 4); tag="U")
circuit = chain(2, put((1, 2) => U))
psi = zero_state(2)
apply!(psi, circuit)

quantum_circuit = Quantum(circuit)
zeros = Quantum(Product(fill([1, 0], 2))) #|00>
ones = Quantum(Product(fill([0, 1], 2))) #|11>

expected_value = Tenet.contract(merge(zeros, quantum_circuit, ones')) # <11|circuit|00>

SV_Yao = apply!(zero_state(2), circuit) # circuit|00>

@test only(expected_value) only(statevec(ArrayReg(bit"11"))' * statevec(SV_Yao))
end
end

0 comments on commit afb85bb

Please sign in to comment.