High level interface
A simulation is described by a SimData, which lists its phases, and run by runTMS, which returns the Simulation it ends with. The High Level Interface page shows it at work.
Running a simulation
TensorMixedStates.runTMS — Function
runTMS(sim_data; max_time = Inf, checkpoint_interval = 0, threading = :dense, output = nothing)run the simulation, see SimData, in a directory named after it, and return the Simulation it ends with. A checkpoint found in the directory is resumed from, if it was written by the same program, prog.jl in the directory, given the same arguments: copying an edited program there, or setting TMS_ACCEPT_PROGRAM=1, resumes it with that one. A file restart in the directory starts the simulation over, and cleanTMS removes the directory. A destination that is a file of the directory, as log or stamp, or a text file given two lists, is refused before the run starts.
max_time: seconds after which the run stops, writing a checkpointcheckpoint_interval: seconds between two checkpoints, 0 for those of a stop onlythreading: how the contractions are threaded, seeset_threading::dense,:blocks,:auto, chosen before each phase from the system of the state, ornothing, leaving the threading as it isoutput: a stream to write everything to,stdoutordevnullfor instance, instead of a directory; nothing can then be resumed
The first three are overridden by the environment variables TMS_MAX_TIME, TMS_CHECKPOINT_INTERVAL and TMS_THREADING, TMS_MAX_TIME=3600 julia prog.jl for instance. The run also stops, writing a checkpoint, when the file stop appears in its directory, and on an interrupt. Runs may be nested or run on several threads: the outermost one sets the threading and the behaviour of Ctrl-C, which the last to end puts back, and an interrupt stops a nested run and the runs it is in, each resuming where it was. A nested run, as in an Update, is done again when the run it is in resumes, the same way with output = devnull and a seed only: in a directory of its own, it resumes its checkpoint or starts over, a seed drawn anew.
TensorMixedStates.stopped — Function
stopped(sim)whether the run runTMS returned stopped before the end of its phases, at max_time, on the file stop or on an interrupt: running it again resumes it
Examples
sim = runTMS(sim_data)
if stopped(sim)
println("stopped at time ", sim.time, ", to be resumed")
endTensorMixedStates.cleanTMS — Function
cleanTMS(sim_data)
cleanTMS(name)remove the directory of a simulation, refused while a run may be using it
Examples
cleanTMS(sim_data)TensorMixedStates.SimData — Type
SimData(; name = "simulation", phases, options...)the description of a simulation, which runTMS runs.
name: the name of the simulation, and of the directory its results are written tophases: the phases, seeTensorMixedStates.AbstractPhase, as a vector which may contain vectors to any depth, flatteneddescription: the text of the filedescriptionof the simulation (default"")time_start: the time the simulation starts at (default 0)final_measurements: the measurements made at the end, seeoutput(default[])format: the Printf format of the numbers written to text files (default"%.13g", thirteen significant digits, the precision below which a difference is taken for rounding)seed: the seed of the random number generator of the run,sim.rng, an integer of zero or above, drawn at random by default and written in the filestamp, so that a run can be made again
A measurement of a value that no phase provides is refused here, see TensorMixedStates.provided_values, and so is a state named by a name the run reserves, or twice in the on of a phase.
Examples
SimData(
name = "ising",
phases = [
CreateState{Pure}(10, Qubit(), "Up"),
GroundState(hamiltonian = -sum(Z(i) * Z(i + 1) for i in 1:9) - sum(X(i) for i in 1:10),
algo = Dmrg(nsweeps = 10), limits = Limits(maxdim = [10, 20, 50]),
final_measurements = "data" => [:energy, X, Z]),
],
)TensorMixedStates.Simulation — Type
Simulation(sim, state[, time = sim.time]; values...)the simulation runTMS runs and returns: a state, its time and its values, which the measurements read, sim.energy as c.energy. The form above gives sim another state, and possibly another time and other values, sharing its run. A value is a number, an array, a state, anything a phase leaves for the measurements and the phases after it; a checkpoint keeps it, so that it must not be changed in place.
sim.state,sim.time: the state and the timesim.step: the steps the phase being run has donesim.rng: the random number generator of the run, for every draw of a phase, seeSimDatasim.name: the valuename, a state named byCopyStatefor instancesim.data: the rows gathered in eachData(name), undername, a table, seeData
Examples
sim = runTMS(sim_data)
rows = sim.data["magnetization"]TensorMixedStates.Data — Type
Data(name)a destination kept in memory, a vector of rows, each the time then the pairs name => value measured, see DataRow, and a table of the cells of these values, see DataTable
TensorMixedStates.data_to_frame — Function
data_to_frame(rows)the DataFrame of the rows of a Data, DataFrame(rows), see DataTable: the column time, then the columns a text table of the same measurements has, in the order they first come, missing in a row without them. The DataFrames package must be loaded.
Examples
using DataFrames
df = data_to_frame(sim.data["magnetization"])Phases
TensorMixedStates.AbstractPhase — Type
abstract type AbstractPhasethe supertype of the phases of a simulation. Every phase has these three fields:
name: the name of the phase, written in the logtime_start: the time the clock is set to when the phase starts (defaultnothing, keeping the time)final_measurements: the measurements to make at the end of the phase, seeoutput(default[])
A phase of one's own is a subtype with these fields and either of two interfaces, runTMS logging it, applying its time_start and making its final measurements:
- a phase made of steps has methods of
TensorMixedStates.prepare_phase,TensorMixedStates.step_countandTensorMixedStates.run_step; the run measures its fieldmeasurements, if it has one, everymeasurements_periodsteps (1 without that field), logs, commits and stops between two steps, and resumes it after the last step committed - a phase of a single call has a method of
TensorMixedStates.run_phase, which callsTensorMixedStates.step_donewherever it can stop
A phase providing values to the measurements, as :energy, declares them with a method of TensorMixedStates.provided_values.
TensorMixedStates.CreateState — Type
CreateState(; type, system, state, randomize, on, name, time_start, final_measurements)
CreateState(state::State; options...)
CreateState{Pure|Mixed}(system, state; options...)
CreateState{Pure|Mixed}(n, site, state; options...)
CreateState{Pure|Mixed}(sites, state; options...)a phase that creates the state of the simulation.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhasetype: the representation of the state,PureorMixed, by default that ofstatewhen it is aStatesystem: theSystemof the state, unused whenstateis aStatestate: a description of the state, seeState, or aState, mixed iftypeasks for itrandomize: the link dimension of a random state to draw with the generator of the run, seerandom_state(default 0, none): a pure state randomized fromstate, a mixed one drawn from a purification starting from it, or withoutstateany state of the systemon: the name the state created takes, seeCopyState(default:state)
Examples
CreateState(type = Pure, system = System(10, Qubit()), state = "Up", randomize = 50)
CreateState(graph_state(System(10, Qubit()), complete_graph(10)))
CreateState{Mixed}(4, Fermion(conserve = N), ["Occ", "Emp", "Occ", "Emp"]; randomize = 16)
CreateState{Pure}(10, Qubit(), "Up")TensorMixedStates.LoadState — Type
LoadState(; file, statename = "state", limits, on = :state, name, time_start,
final_measurements)a phase that loads a state from an HDF5 file written by SaveState or save_state, see load_state, and truncates it to limits, unless they are the default Limits().
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhasefile: the HDF5 file, taken in the directory of the simulation when it is relative: a state another simulation saved is found under../othername/statename: the name of the state in the filelimits: the truncation of the state once loaded, seeLimitson: the name the state takes in the simulation,:statefor the current state, another for a reference state,c.refto the measurements, seeFidelity
Examples
LoadState(file = "ground.h5")
LoadState(file = "../other/ground.h5", statename = "gs", on = :ground)TensorMixedStates.SaveState — Type
SaveState(; file, statename = "state", on = :state, name, time_start, final_measurements)a phase that saves a state in an HDF5 file, see save_state, replacing a state saved there under the same name.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhasefile: the HDF5 file, taken in the directory of the simulation when it is relative, not one of the files of the simulation, aslog, which the run refuses before it startsstatename: the name of the state in the fileon: the state saved,:statethe current one, or one the simulation named
Examples
SaveState(file = "states.h5", statename = "after_quench")TensorMixedStates.CopyState — Type
CopyState(from => to; move = false, name, time_start, final_measurements)a phase that copies the state named from under the name to, :state naming the current state: CopyState(:state => :ground) keeps the current state as :ground, which the measurements read as c.ground, see Fidelity, and CopyState(:ground => :state) makes it the current state again. With move = true, from is removed, which frees its memory and its place in the checkpoints. A copy costs nothing, the two names sharing the state.
Examples
CopyState(:state => :ground)
CopyState(:ground => :state; move = true)TensorMixedStates.Update — Type
Update(f; nsteps = 1, provides = [], measurements, measurements_period, name, time_start,
final_measurements)a phase that changes the simulation by the function f(c), which reads it as a measurement reads its context, c.state, c.time, c.step, c.rng, c.ground, and changes it by assignments:
c.state = st,c.time = t: the state and the timec.x = v: a value of the simulation, measured and kept by the checkpoints,c.x = nothingremoving it, which frees the memory of a state
The phase makes nsteps steps, the run measuring measurements every measurements_period steps, committing and stopping between two, as for Evolve, c.step being the steps done before. A stopped run resumes after the last step committed: f must draw with c.rng alone and act only through c, output(c, …) and log_message(c, …), what it changes elsewhere being neither kept nor done again. f works on a copy, which a failure leaves the simulation without.
provides: the names of the valuesfsets, which the measurements may then read, seeTensorMixedStates.provided_values
Examples
Update(c -> c.ground = c.state; provides = [:ground])
Update(c -> c.ground = nothing)
Update(nsteps = 1000, provides = [:m], measurements = "kicks" => [Z(1), :m],
measurements_period = 10) do c
c.m, _, c.state = collapse(apply(kick, c.state), Z(1); rng = c.rng)
endTensorMixedStates.ToMixed — Type
ToMixed(; limits, on, name, time_start, final_measurements)a phase that takes the state to the mixed representation, mix(state; limits), truncated to limits unless they are the default Limits(); a state already mixed is only truncated.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhaselimits: the truncation of the mixed state, seeLimitson: the name of the state the phase acts on, seeCopyState(default:state)
Examples
ToMixed(limits = Limits(cutoff = 1e-10, maxdim = 10))TensorMixedStates.Weaken — Type
Weaken(; target, on, name, time_start, final_measurements)a phase that takes the state down to a lower level of conservation, see weaken: a phase may evolve under a strong symmetry, and the next one under a weak one, where a jump that moves the charge becomes possible.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhasetarget: what the state must still conserve, asweakentakes it (defaultnothing, one level down)on: the name of the state the phase acts on, seeCopyState(default:state)
Examples
Weaken()
Weaken(target = ())TensorMixedStates.PartialTrace — Type
PartialTrace(; keep | trace_out, on, name, time_start, final_measurements)a phase that reduces the state to the sites at the positions keep, in increasing order, or traces out those at trace_out, see partial_trace. The sites kept make a new system, numbered from 1 in the order of the chain, which the operators of the phases after this one refer to. on is the name of the state it acts on, see CopyState (default :state).
Examples
PartialTrace(trace_out = [2, 3, 6])
PartialTrace(keep = [1, 4, 5])TensorMixedStates.Gates — Type
Gates(; gates, limits, on, name, time_start, final_measurements)a phase that applies gates to the state, see apply, by its steps, a batch of gates or an MPO, between which the run can write a checkpoint or stop.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhasegates: the gates, an operator or a channel placed on siteslimits: the truncation, seeLimitson: the name of the state the phase acts on, seeCopyState, or a vector of names, each state acted on alike (default:state)
Examples
Gates(gates = controlled(X)(1, 3) * controlled(Z)(2, 4), limits = Limits(maxdim = 20))TensorMixedStates.Evolve — Type
Evolve(; evolver, duration, time_step | nsteps, algo = Tdvp(), limits, mpo_limits,
measurements, measurements_period, on, name, time_start, final_measurements)a phase of time evolution, see evolve.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhaseevolver:-im * Hfor a HamiltonianH, plus dissipators for a mixed state, or for an evolver depending on time a vector of terms[A, B => g], seeevolve, the time being that of the simulationduration: the duration of the evolutiontime_step: the time step, adjusted to the nearest one dividing the duration into a whole number of steps, and taken with the sign of the duration (no step when that number is zero), ornsteps, the number of steps, one of the two being givenalgo: the method, seeEvolutionAlgorithmlimits: the limits of the state, seeLimitsmpo_limits: the truncation of the MPO of the evolver, seePreMPO(default none)measurements: the measurements made after everymeasurements_periodsteps (default 1), seeoutput; the state the phase starts from is measured by the final measurements of the phase beforeon: the name of the state the phase evolves, seeCopyState, or a vector of names, the states of one system and one representation evolved together under the evolver, the time advancing once, for two time correlations for instance (default:state)
Examples
Evolve(evolver = -im * (Z(1) * Z(2) + Z(2) * Z(3)), duration = 2., time_step = 0.1,
measurements = "data" => [X, Y, Z])TensorMixedStates.Thermalize — Type
Thermalize(; hamiltonian, beta, beta_step | nsteps, algo = Tdvp(), limits, mpo_limits,
measurements, measurements_period, on, name, time_start, final_measurements)a phase that takes a mixed state $\rho$ to $e^{-\beta H/2} \rho \, e^{-\beta H/2}$, normalized to trace one, by an evolution in imaginary time, see thermalize: from "FullyMixed", the thermal state $e^{-\beta H}/Z$. The time does not move. It provides the values :beta, the inverse temperature reached, and :log_trace, the logarithm of the trace before normalization, see thermalize: 0 both for a beta shorter than half a step, which leaves the state normalized only.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhasehamiltonian: the Hamiltonian $H$beta: the inverse temperature $\beta$beta_step: its step, adjusted as the time step ofEvolve, ornsteps, the number of steps, one of the two being givenalgo: the method, seeEvolutionAlgorithmlimits: the limits of the state, seeLimitsmpo_limits: the truncation of the MPO of the Hamiltonian, seePreMPO(default none)measurements: the measurements made after everymeasurements_periodsteps (default 1)on: the name of the state the phase acts on, seeCopyState(default:state)
Examples
Thermalize(hamiltonian = -sum(Z(i) * Z(i + 1) for i in 1:9) - sum(X(i) for i in 1:10),
beta = 2., beta_step = 0.1, limits = Limits(maxdim = 64),
measurements = "thermo" => [:beta, :log_trace, Z(5)])TensorMixedStates.GroundState — Type
GroundState(; hamiltonian, algo, limits, mpo_limits, orthogonal_to, weight, measurements,
measurements_period, on, name, time_start, final_measurements)a phase that searches the ground state of a Hamiltonian from the pure state, see ground_state, or an excited state, the value :energy taking the energy of each sweep.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhasehamiltonian: the Hamiltonianalgo: the method, seeSearchAlgorithmlimits: the limits of the sweeps, seeLimits, which may give one value per sweepmpo_limits: the truncation of the MPO of the Hamiltonian, seePreMPO(default none)orthogonal_to: the eigenstates the state searched is orthogonal to, by their names in the simulation, seeCopyState, or given, andweight, seeground_state(default none)measurements: the measurements made after everymeasurements_periodsweeps (default 1)on: the name of the state the phase acts on, seeCopyState(default:state)
Examples
GroundState(hamiltonian = X(1) * X(2), algo = Dmrg(nsweeps = 10, tol = 1e-10),
limits = Limits(maxdim = [10, 20, 30]))
[GroundState(hamiltonian = H, algo = Dmrg(nsweeps = 10)), CopyState(:state => :ground),
GroundState(hamiltonian = H, algo = Dmrg(nsweeps = 20), orthogonal_to = [:ground])]TensorMixedStates.SteadyState — Type
SteadyState(; lindbladian, algo, limits, mpo_limits, measurements, measurements_period, on,
name, time_start, final_measurements)a phase that searches the steady state of a Lindbladian from the mixed state, see steady_state, the value :residual taking the eigenvalue of $L^\dagger L$ of each sweep, 0 for a steady state, and the state measured after a sweep having trace one. Its lists of measurements cannot read :energy, which would be that of another phase.
name,time_start,final_measurements: the fields of every phase, seeTensorMixedStates.AbstractPhaselindbladian: the Lindbladian $L$,-im * Hplus dissipatorsalgo: the method, seeSearchAlgorithmlimits: the limits of the sweeps, seeLimitsmpo_limits: the truncation of the MPO of $L^\dagger L$, seePreMPO(default none)measurements: the measurements made after everymeasurements_periodsweeps (default 1)on: the name of the state the phase acts on, seeCopyState(default:state)
Examples
SteadyState(lindbladian = -im * H + D, algo = Dmrg(nsweeps = 10, tol = 1e-5),
limits = Limits(maxdim = [10, 20, 50]))Phases, methods, destinations and representations of one's own
How to use them is explained in Extending TMS.
TensorMixedStates.prepare_phase — Function
TensorMixedStates.prepare_phase(phase, sim)
TensorMixedStates.step_count(prepared)
TensorMixedStates.run_step(prepared, sim, k)the interface of a phase made of steps: prepare_phase returns what the steps need, from the simulation as the phase starts, step_count the number of steps, and run_step makes step k, returning the simulation it leaves, see Simulation. On a resume, prepare_phase is given the state of the last step committed, with the time the phase started from, and the steps go on after it: what it prepares may depend on the system of the state, not on the state.
Examples
struct Kicks <: TensorMixedStates.AbstractPhase
name::String
time_start
final_measurements
nkicks::Int
measurements
end
TensorMixedStates.prepare_phase(p::Kicks, _) = p
TensorMixedStates.step_count(p::Kicks) = p.nkicks
TensorMixedStates.run_step(::Kicks, sim, _) =
Simulation(sim, apply(exp(-0.3im * X)(1), sim.state), sim.time + 0.1)TensorMixedStates.run_phase — Function
TensorMixedStates.run_phase(phase, sim, done)run phase on the simulation sim, its first done steps already done, and return the simulation it leaves. For a phase made of steps, it prepares the phase and runs the steps after done, ending each with step_done; a phase of a single call has a method of its own.
TensorMixedStates.step_done — Function
TensorMixedStates.step_done(sim, phase, k; last = false, resume_from = sim)the end of step k of phase, the simulation sim holding what it reached: its measurements, when due, and its line in the log, then the commit of the step, a checkpoint when one is due, and the stop of the run when one is asked for. It returns the simulation, its value step set to k. A phase of a single call calls it wherever it can stop, a sweep of a search for instance: its run_phase(phase, sim, done) is then called with the steps already done and the simulation committed, resume_from, which is sim unless the phase goes on from another state than the one measured, as a search measuring its eigenvector divided by its trace. With last, the phase ends there, sim holding its result, which a resumed run takes without running the phase again (a search ending on its tolerance would otherwise go on): resume_from is then sim.
TensorMixedStates.provided_values — Function
TensorMixedStates.provided_values(phase)the names of the values phase gives the simulation, (:energy,) for GroundState for instance, none by default. A measurement of a value that no phase provides is refused when the SimData is written, and one that is not there yet is missing.
TensorMixedStates.evolve_step — Function
evolve_step(algo, pre, state, time, dt, step, limits)the state after one step of dt from time by the method algo, under the evolver pre, whose time functions are pre.functions, one per term, nothing when it does not depend on time, see make_mpo, step being the number of the step, from 1, and limits its limits, see step_limits. An EvolutionAlgorithm of one's own adds a method.
TensorMixedStates.search — Function
search(algo, mpo, state, limits; sweep_done = nothing, krylov = Krylov(), done = 0,
energy = nothing[, orthogonal_to, weight])the lowest eigenvalue of mpo and its eigenvector, normalized, searched from state by the method algo, limits the limits of the sweeps, see Limits; sweep_done(sweep, energy, state, ends) is called at the end of each sweep, ends telling whether the search ends there, on its tolerance or its last sweep, with the state it returns, and krylov gives the defaults of the method of the caller. A search resumed after done sweeps takes the rest of the schedules, energy being that of the last sweep, which it returns when no sweep is left. Given orthogonal_to, pure states on the system of state, the search is that of the lowest eigenvector orthogonal to them, which dmrg makes by adding weight times their projectors to mpo, the energy being that of mpo alone. A single site, which dmrg does not take, is diagonalized whole, in one sweep, see lowest_on_site. A SearchAlgorithm of one's own adds a method, taking these two keywords only if it searches excited states.
TensorMixedStates.write_row — Function
write_row(o, time, row)write to the destination opened o the values row, pairs name => value, measured at time, see Destination
TensorMixedStates.check_list — Function
check_list(o, names)refuse a list of measurements, by their names, that the destination opened o cannot take, see Destination; it takes any by default
TensorMixedStates.resume_mark — Function
resume_mark(o)the mark that a resumed simulation reopens the destination opened o at, open(d, mark), see Destination; nothing by default, the destination then starting anew
TensorMixedStates.write_state — Function
write_state(group, state)write the tensors of state in the HDF5 group group, where save_state writes its type and its sites: the MPS of a State. The state of an extension needs a method of its own, and one of read_state.
TensorMixedStates.read_state — Function
read_state(::Type{S}, group, sites)the state of type S that save_state wrote in the HDF5 group group, on the sites sites. The state of an extension needs a method of its own, reading what its write_state wrote.
TensorMixedStates.expect_value — Function
expect_value(state, op)the expectation value of the operator op placed on sites, as the state computes it, divided by the real part of its trace: the number expect gives with complex = true, and makes real for a Hermitian operator otherwise. The state of an extension adds a method, which expect, expect1, expect2 and measure go through, see AbstractState.