julia/QuantumLab Cheat Sheet

Getting started
Pkg.add("QuantumLab")
using QuantumLab
DataTypes
Group Keyword Explanation
Data Types abstract only for internal nodes of the type-tree
Data Types bitstype N basal type of length N bits
Data Types type like a struct in C
Data Types immutable type that is totally defined by the values of its fields
Other Types TupleTypes e.g. (a b c), covariant with a,b,c
Other Types UnionTypes (e.g. Union(a b c))
Parametric Types type ...{T} invariant with T
Parametric Types abstract ...{T}
Parametric Types Type{T} only T <: Type{T}
Parametric Types bitstype N ...{T} T is only an abstract tag
Typealias typealias e.g. typealias Matrix{T} Array{T 2}
Optimizations
  • Avoid global variables (at least declare them const or annotate their types at the point of use)
  • Always at least @time and pay attention to memory allocations (they might be a sign for type instability)
  • @profile and ProfileView.view()
  • @allocated or julia –track-allocation=user
  • @code_warntype
  • Lint and TypeCheck (if they work)
  • Don’t use abstract types as parameters to parametric types (including Arrays)
  • Don’t use abstract types for fields of a type (rather make the type parametric instead)
  • When you know the explicit type to an abstract type given to your function, declare it on first use (this automatically makes for a runtime type check)
  • sizehint!
  • for i in 1:3 getfield(obj,i) end is faster than for f in fieldnames(obj) obj.(f) end
  • push! is faster than append! when adding a single element
Tips and Tricks
  • symlink /usr/share/julia/base (or equivalent on other OSs) to an easy to access location for quick reference (e.g. ~/juliabase)
  • ~/juliabase/latex_symbols.jl is the reference for latex-codes (unicode input)
  • adam-p has a comprehensive Markdown-Cheatsheet

 

 

Module Import vs. Using
if MyModule exports x but a not necessarily:
Import Command Into Scope
using MyModule x; MyModule.a
using MyModule.a a
using MyModule:a a
import MyModule MyModule.a
import MyModule.a a
import MyModule:a a
importall MyModule x
Module Search Path
Command Search Location
using Modulename $PATH/*
using("Modulename") $pwd/Modulename.jl
Control Flow
Expression Type Code
Compound Expressions begin and (;).
Conditional Evaluation if-elseif-else and ?: (ternary operator).
Short-Circuit Evaluation &&, || and chained comparisons.
Repeated Evaluation, Loops while and for.
Exception Handling try-catch, error() and throw().
Tasks (aka Coroutines) yieldto().
Representation Layers

Name obtained by comment
1 AST :f
2 Lowered AST @code_lowered f(x) desugared and unnested
3 Typed AST @code_typed f(x) type-inferred and optimized
4 LLVM @code_llvm f(x)
5 Assembly @code_native f(x) machine-specific