Skip to content

Instantly share code, notes, and snippets.

@SharzyL
Last active September 5, 2020 08:59
Show Gist options
  • Save SharzyL/c6465adfa8551c565da34a56e4920b68 to your computer and use it in GitHub Desktop.
Save SharzyL/c6465adfa8551c565da34a56e4920b68 to your computer and use it in GitHub Desktop.
A Mathematica script to calculate the matrix representation of quantum gate.
depth = 3;
gate[func_] := Table[Flatten[func @@ IntegerDigits[l, 2, depth]], {l, 0, 2^depth - 1}];
v[i_] := If[EvenQ[i], {1, 0}, {0, 1}];
h[i_] := 1/Sqrt[2] (v[0] + (1 - 2 i) v[1]);
t[i_] := If[EvenQ[i], v[0], E^(I \[Pi]/4) v[1]];
tc[i_] := If[EvenQ[i], v[0], E^(-I \[Pi]/4) v[1]];
s[i_] := If[EvenQ[i], v[0], I v[1]];
gates = {
gate[TensorProduct[v[#1], v[#2], h[#3]] &],
gate[TensorProduct[v[#1], v[#2], v[#2 + #3]] &],
gate[TensorProduct[v[#1], v[#2], tc[#3]] &],
gate[TensorProduct[v[#1], v[#2], v[#1 + #3]] &],
gate[TensorProduct[v[#1], v[#2], t[#3]] &],
gate[TensorProduct[v[#1], v[#2], v[#2 + #3]] &],
gate[TensorProduct[v[#1], v[#2], tc[#3]] &],
gate[TensorProduct[v[#1], v[#2], v[#1 + #3]] &],
gate[TensorProduct[v[#1], tc[#2], t[#3]] &],
gate[TensorProduct[v[#1], v[#1 + #2], v[#3]] &],
gate[TensorProduct[v[#1], tc[#2], v[#3]] &],
gate[TensorProduct[v[#1], v[#1 + #2], v[#3]] &],
gate[TensorProduct[t[#1], s[#2], h[#3]] &]
};
Fold[Dot, IdentityMatrix[2^depth], gates] // MatrixForm
gate[TensorProduct[v[#1], v[#2], v[#3 + #1 #2]] &] // MatrixForm
TeXForm /@ gates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment