Skip to content

Commit fc8225d

Browse files
committed
Added unit tests for Housholder decomposition
1 parent 95d1307 commit fc8225d

File tree

1 file changed

+121
-2
lines changed

1 file changed

+121
-2
lines changed

Unit_tests.m

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,68 @@
15981598
)]
15991599

16001600

1601+
(*Unit tests for (sparse and dense) Housholder decomposition*)
1602+
1603+
1604+
(*Check Methods based on Householder decomposition*)
1605+
1606+
1607+
checkDenseHouseholder[v_]:=Module[{st,iso},(
1608+
st=DenseHouseholderDec[v];
1609+
iso=NCreateOperationFromGateList[st];
1610+
isIdentityUpToPhase[N[ConjugateTranspose[v].iso]]
1611+
)
1612+
]
1613+
checkSparseHouseholder[v_]:=Module[{st,iso},(
1614+
st=SparseHouseholderDec[v];
1615+
iso=NCreateOperationFromGateList[st];
1616+
isIdentityUpToPhase[N[ConjugateTranspose[v].iso]]
1617+
)
1618+
]
1619+
1620+
1621+
(*Tests for (sparse and dense) Householder decomposition*)
1622+
1623+
1624+
TestDenseHouseholder:=Module[{error},(
1625+
error=0;
1626+
If[Quiet[Check[checkDenseHouseholder[PickRandomIsometry[2,2^4]],error=1;False]]&&
1627+
Quiet[Check[checkDenseHouseholder[PickRandomIsometry[1,2^3]],error=2;False]]&&
1628+
Quiet[Check[checkDenseHouseholder[randPermutMat[2^1,2^2]],error=3;False]]&&
1629+
Quiet[Check[checkDenseHouseholder[N[randPermutMat[2^2,2^4]]],error=4;False]]&&
1630+
Quiet[Check[checkDenseHouseholder[PickRandomIsometry[1,2^2]],error=5;False]]&&
1631+
Quiet[Check[checkDenseHouseholder[PickRandomIsometry[2^3,2^3]],error=6;False]],
1632+
True,
1633+
Print["Error in DenseHouseholderDec with error message code ",error];False
1634+
]
1635+
)]
1636+
1637+
1638+
TestSparseHouseholder:=Module[{error},(
1639+
error=0;
1640+
If[Quiet[Check[checkSparseHouseholder[PickRandomIsometry[2,2^4]],error=1;False]]&&
1641+
Quiet[Check[checkSparseHouseholder[PickRandomIsometry[1,2^3]],error=2;False]]&&
1642+
Quiet[Check[checkSparseHouseholder[randPermutMat[2^1,2^2]],error=3;False]]&&
1643+
Quiet[Check[checkSparseHouseholder[N[randPermutMat[2^2,2^4]]],error=4;False]]&&
1644+
Quiet[Check[checkSparseHouseholder[PickRandomSparseIsometry[1,2^2]],error=5;False]]&&
1645+
Quiet[Check[checkSparseHouseholder[PickRandomSparseIsometry[2^3,2^3]],error=6;False]],
1646+
True,
1647+
Print["Error in SparseHouseholderDec with error message code ",error];False
1648+
]
1649+
)]
1650+
1651+
1652+
(*All tests for Housholder decomposition*)
1653+
1654+
1655+
testHouseholderAll := Module[{},(
1656+
If[TestDenseHouseholder&&TestSparseHouseholder,
1657+
Print["All tests for (dense and sparse) Householder decomposition pass."],,
1658+
Print["testHouseholderAll neither returned True nor False"]
1659+
]
1660+
)]
1661+
1662+
16011663
(*Unit tests for state preparaion (Plesch-Brukner decomposition)*)
16021664

16031665

@@ -1634,12 +1696,61 @@
16341696

16351697
testStatePreparationAll := Module[{},(
16361698
If[TestStatePreparation,
1637-
Print["All tests for State preparation pass."],,
1699+
Print["All tests for state preparation pass."],,
16381700
Print["testStatePreparationAll neither returned True nor False"]
16391701
]
16401702
)]
16411703

16421704

1705+
(*Unit tests for sparse state preparaion*)
1706+
1707+
1708+
(*Checks for sparse state preparation*)
1709+
1710+
1711+
checkSparseStatePreparation[psi_]:=Module[{st,iso},(
1712+
st=SparseStatePreparation[psi];
1713+
iso=NCreateOperationFromGateList[st];
1714+
isIdentityUpToPhase[N[ConjugateTranspose[psi].iso]]
1715+
)
1716+
]
1717+
1718+
1719+
(*Tests for sparse state preparation*)
1720+
1721+
1722+
TestSparseStatePreparation:=Module[{error},(
1723+
error=0;
1724+
If[Quiet[Check[checkSparseStatePreparation[PickRandomPsi[2^4]],error=1;False]]&&
1725+
Quiet[Check[checkSparseStatePreparation[PickRandomSparsePsi[2^4,2^3]],error=2;False]]&&
1726+
Quiet[Check[checkSparseStatePreparation[PickRandomPsi[2^4]],error=3;False]]&&
1727+
Quiet[Check[checkSparseStatePreparation[N[randPermutMat[1,8]]],error=1;False]]&&
1728+
Quiet[Check[checkSparseStatePreparation[N[randPermutMat[1,8]]],error=2;False]]&&
1729+
Quiet[Check[checkSparseStatePreparation[N[randPermutMat[1,8]]],error=3;False]]
1730+
,
1731+
True,
1732+
Print["Error in SparseStatePreparation with error message code ",error];False
1733+
]
1734+
)]
1735+
1736+
1737+
(*All tests for sparse state preparation*)
1738+
1739+
1740+
testSparseStatePreparationAll := Module[{},(
1741+
If[TestSparseStatePreparation,
1742+
Print["All tests for sparse state preparation pass."],,
1743+
Print["testSparseStatePreparationAll neither returned True nor False"]
1744+
]
1745+
)]
1746+
1747+
1748+
(*Unit tests for CNOT \[TwoWayRule] XX transformation*)
1749+
1750+
1751+
(*Checks for CNOT \[TwoWayRule] XX transformation*)
1752+
1753+
16431754
CheckCNOTtoXX[v_]:=Module[{st,st2,iso,ch},
16441755
st=DecIsometryGeneric[v];st2=CNOTRotationsToXXRGates[st];
16451756
iso=CreateOperationFromGateList[st2,Log[2,Dimensions[v][[1]]]];ch=CT[v].iso;
@@ -1651,6 +1762,10 @@
16511762
iso=CreateOperationFromGateList[st3,Log[2,Dimensions[v][[1]]]];ch=CT[v].iso;
16521763
Chop[ch/ch[[1,1]]-IdentityMatrix[Dimensions[iso][[2]]],10^-6]==0*IdentityMatrix[Dimensions[iso][[2]]]]
16531764

1765+
1766+
(*Tests for CNOT \[TwoWayRule] XX transformation*)
1767+
1768+
16541769
TestCNOTtoXX:=Module[{error=0},If[Quiet[Check[CheckCNOTtoXX[PickRandomIsometry[2^2,2^2]],error=1;False]]&&
16551770
Quiet[Check[CheckCNOTtoXX[PickRandomIsometry[2^2,2^3]],error=2;False]]&&
16561771
Quiet[Check[CheckCNOTtoXX[PickRandomIsometry[2^2,2^4]],error=3;False]]&&
@@ -1675,6 +1790,10 @@
16751790
Print["Error in CheckXXtoCNOT with error message code ",error];False
16761791
]]
16771792

1793+
1794+
(*All tests for CNOT \[TwoWayRule] XX transformation*)
1795+
1796+
16781797
testXXCNOTAll := Module[{},(
16791798
If[TestCNOTtoXX&&TestXXtoCNOT,
16801799
Print["All tests for converting to and from XX gates pass."],,
@@ -1752,7 +1871,7 @@
17521871

17531872

17541873
(*Run all tests*)
1755-
runAllTests:=Module[{},(testAllBasicMethods;testUCGs;testAllDiagGateMethods;testIsoSmall;testCCDec;testDec2Qubit;testDecSingleQubit;testQSDAll;testQSD;testStatePreparationAll;testAllMCGMethods;testKnill;testIsometryDecompositions;testStinespring;testPOVM;testXXCNOTAll;testChannelDecompositions;testInstrumentDecompositions)]
1874+
runAllTests:=Module[{},(testAllBasicMethods;testUCGs;testAllDiagGateMethods;testIsoSmall;testCCDec;testDec2Qubit;testDecSingleQubit;testQSDAll;testQSD;testStatePreparationAll;testSparseStatePreparationAll;testAllMCGMethods;testKnill;testHouseholderAll;testIsometryDecompositions;testStinespring;testPOVM;testXXCNOTAll;testChannelDecompositions;testInstrumentDecompositions)]
17561875

17571876

17581877
Timing[runAllTests]

0 commit comments

Comments
 (0)