Skip to content

Instantly share code, notes, and snippets.

@MasonProtter
Last active July 10, 2020 18:07
Show Gist options
  • Select an option

  • Save MasonProtter/4c5de8afc431dea3596cf803bc91fffb to your computer and use it in GitHub Desktop.

Select an option

Save MasonProtter/4c5de8afc431dea3596cf803bc91fffb to your computer and use it in GitHub Desktop.

Revisions

  1. MasonProtter revised this gist Jul 10, 2020. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion braket.jl
    Original file line number Diff line number Diff line change
    @@ -33,4 +33,20 @@ Bra(a::Vector) = a'
    #+RESULTS:
    : 2×2 Array{Int64,2}:
    : 1 0
    : 0 -1
    : 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
  2. MasonProtter created this gist Jul 10, 2020.
    36 changes: 36 additions & 0 deletions braket.jl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #+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