Created
June 7, 2022 16:22
-
-
Save lucainnocenti/bccab7fae7777f7a75ece444af1b407b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generatePair[pauliIndices_List, tuple_List] := { | |
BitXor[ | |
ReplaceAll[ | |
pauliIndices, {3 -> 0, 2 -> 1} | |
], | |
tuple | |
], | |
(-1)^Total@tuple[[Flatten@Position[pauliIndices, 2 | 3]]] | |
}; | |
matrixElementsFromPauliTuple[matrix_, pauliIndices_] := Table[ | |
{ | |
#[[2]], | |
{FromDigits[tuple, 2] + 1, FromDigits[#[[1]], 2] + 1} | |
} &@generatePair[pauliIndices, tuple], | |
{tuple, Tuples[{0, 1}, Length@pauliIndices]} | |
] // Transpose // Total@Times[ | |
matrix[[Sequence @@ #]] & /@ #[[2]], | |
#[[1]] | |
] &; | |
coefficientsFromMatrix[matrix_] := Table[ | |
matrixElementsFromPauliTuple[matrix, | |
IntegerDigits[idx - 1, 4, Log[2, Length@matrix]] | |
], | |
{idx, 4^Log[2, Length@matrix]} | |
]/Length@matrix; | |
matrixElementsFromPauliTuple[ | |
IdentityMatrix@4, | |
{0, 0} | |
] | |
coefficientsFromMatrix[IdentityMatrix@4] | |
coefficientsFromMatrix[IdentityMatrix@8] // Length |
direct method with LinearSolve:
{sparsePaulis[0], sparsePaulis[1], sparsePaulis[2], sparsePaulis[3]} = SparseArray /@ PauliMatrix /@ {0, 1, 2, 3};
integerToPauliTuple[integer_, numQubits_] := IntegerDigits[integer - 1, 4, numQubits];
matrixOfPaulis[numQubits_] := matrixOfPaulis[numQubits] = Transpose@SparseArray@Table[
KroneckerProduct @@ (sparsePaulis /@ integerToPauliTuple[idx, numQubits]) // Flatten,
{idx, 4^numQubits}
];
randomMat[numQubits_] := RandomReal[{-1, 1}, 2^numQubits {1, 1}];
LinearSolve[matrixOfPaulis@8, Flatten@randomMat@8]
This manages to solve up to 8 qubits, in roughly 8 seconds. Hangs for more than a minute for 9 qubits.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alternative "standard" method:
Roughly 4s to run with 7 qubits. Roughly 35s for 8 qubits.