Skip to content

Instantly share code, notes, and snippets.

@MasonProtter
Last active July 10, 2020 18:07
Show Gist options
  • Save MasonProtter/4c5de8afc431dea3596cf803bc91fffb to your computer and use it in GitHub Desktop.
Save MasonProtter/4c5de8afc431dea3596cf803bc91fffb to your computer and use it in GitHub Desktop.
#+BEGIN_SRC julia
const ketpat = r"\|.*?\>"
ketrep(str) = "Ket("*(match(r"(?<=\|).*?(?=>)", str).match)*")"
const brapat = r"\<.*?\|"
brarep(str) = "Bra("*(match(r"(?<=<).*?(?=\|)", str).match)*")"
function rep_braket(str)
replace(replace(str, brapat => brarep), ketpat => ketrep)
end
parse_braket(str) = Meta.parse(rep_braket(str))
macro d_cmd(str)
esc(parse_braket(str))
end
Ket(args...) = [args...]
Ket(a::Vector) = a
Bra(args...) = [args...]'
Bra(a::Vector) = a'
Ket(a::Vector) = a
Bra(a::Vector) = a'
↑ = [1, 0]
↓ = [0, 1];
σz = d`|↑>*<↑| - |↓>*<↓|`
#+END_SRC
#+RESULTS:
: 2×2 Array{Int64,2}:
: 1 0
: 0 -1
#+BEGIN_SRC julia
→ = d`(|↑⟩ + |↓⟩)/√2`
← = d`(|↑⟩ - |↓⟩)/√2`
d```
[⟨→|*σz*|→⟩ ⟨→|*σz*|←⟩
⟨←|*σz*|→⟩ ⟨←|*σz*|←⟩]
```
#+END_SRC
#+RESULTS:
: 2×2 Array{Float64,2}:
: 0.0 1.0
: 1.0 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment