def add_current_label(): if cur_format == "StateVec": ar = _evalRowList(cur_rows, bComplex=True) if ar.shape == (1, 2): spam_vecs[cur_label] = _tools.state_to_pauli_density_vec( ar[0, :]) else: raise ValueError("Invalid state vector shape for %s: %s" % (cur_label, ar.shape)) elif cur_format == "DensityMx": ar = _evalRowList(cur_rows, bComplex=True) if ar.shape == (2, 2) or ar.shape == (4, 4): spam_vecs[cur_label] = _tools.stdmx_to_ppvec(ar) else: raise ValueError("Invalid density matrix shape for %s: %s" % (cur_label, ar.shape)) elif cur_format == "PauliVec": spam_vecs[cur_label] = _np.transpose( _evalRowList(cur_rows, bComplex=False)) elif cur_format == "UnitaryMx": ar = _evalRowList(cur_rows, bComplex=True) if ar.shape == (2, 2): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_1q(ar)) elif ar.shape == (4, 4): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_2q(ar)) else: raise ValueError("Invalid unitary matrix shape for %s: %s" % (cur_label, ar.shape)) elif cur_format == "UnitaryMxExp": ar = _evalRowList(cur_rows, bComplex=True) if ar.shape == (2, 2): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_1q(_expm(-1j * ar))) elif ar.shape == (4, 4): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_2q(_expm(-1j * ar))) else: raise ValueError( "Invalid unitary matrix exponent shape for %s: %s" % (cur_label, ar.shape)) elif cur_format == "PauliMx": gs.gates[cur_label] = _objs.FullyParameterizedGate( _evalRowList(cur_rows, bComplex=False))
def Ising_tmatrix(nspins, alpha=0.1, gamma=0.95, ratematrix=False): """ Implements Glaubers master equation variant of the (1D) Ising model with periodic boundary conditions (J Math Phys 4 294 (1963); doi: 10.1063/1.1703954) nspins: number of spins in model (Note: returns 2^nspins times 2^nspins matrix) alpha: basal spin flip-rate, defines time-scale (=0.1) gamma: gamma is equal to tanh(\beta 2J) where J is the spin-spin coupling constant in a corresponding Ising model, and \beta is the inverse temperature. ratematrix: return rate matrix as well """ W = _np.zeros((2**nspins, 2**nspins)) for i, s in enumerate(_itrt.product([-1, 1], repeat=nspins)): s = _np.array(s) for j, c in enumerate(_itrt.product([-1, 1], repeat=nspins)): c = _np.array(c) if _np.all(s == c): #Diagonal is filled later. continue else: flipped = _np.where(s != c)[0] if len(flipped) == 1: f = flipped[0] W[i, j] = 0.5 * alpha * (1. - 0.5 * gamma * s[f] * (s[f - 1] + s[(f + 1) % nspins])) else: pass #fill diagonal W[_np.diag_indices(2**nspins)] = -W.sum(axis=1) #compute transition matrix T = _expm(W) if ratematrix: return T, W else: return T
def get_liouville_mx(obj, prefix=""): """ Process properties of `obj` to extract a single liouville representation """ props = obj['properties']; lmx = None if prefix + "StateVec" in props: ar = _evalRowList(props[prefix + "StateVec"], bComplex=True) if ar.shape == (1, 2): stdmx = _tools.state_to_stdmx(ar[0, :]) lmx = _tools.stdmx_to_vec(stdmx, basis) else: raise ValueError("Invalid state vector shape for %s: %s" % (cur_label, ar.shape)) elif prefix + "DensityMx" in props: ar = _evalRowList(props[prefix + "DensityMx"], bComplex=True) if ar.shape == (2, 2) or ar.shape == (4, 4): lmx = _tools.stdmx_to_vec(ar, basis) else: raise ValueError("Invalid density matrix shape for %s: %s" % (cur_label, ar.shape)) elif prefix + "LiouvilleVec" in props: lmx = _np.transpose(_evalRowList(props[prefix + "LiouvilleVec"], bComplex=False)) elif prefix + "UnitaryMx" in props: ar = _evalRowList(props[prefix + "UnitaryMx"], bComplex=True) lmx = _tools.change_basis(_tools.unitary_to_process_mx(ar), 'std', basis) elif prefix + "UnitaryMxExp" in props: ar = _evalRowList(props[prefix + "UnitaryMxExp"], bComplex=True) lmx = _tools.change_basis(_tools.unitary_to_process_mx(_expm(-1j * ar)), 'std', basis) elif prefix + "LiouvilleMx" in props: lmx = _evalRowList(props[prefix + "LiouvilleMx"], bComplex=False) if lmx is None: raise ValueError("No valid format found in %s" % str(list(props.keys()))) return lmx
def advance(self, state, v, t): state = _np.array(state, dtype='complex') omega, phase, detuning, dephasing, decoherence = v H = (omega * _np.cos(phase) * self.Hx + omega * _np.sin(phase) * self.Hy + detuning * self.Hz) L = dephasing * self.dephasing_generator + decoherence * self.decoherence_generator process = change_basis(_expm((H + L) * t), 'pp', 'col') state = unvec(_np.dot(process, vec(_np.outer(state, state.conj())))) return state
def _exp_gate(arg,dH,d): ##### # REMARK : exp(+arg dH) is before contracted with s.. # _|_|_ Wu,l Wu,l+1 # |___| # | | Wd,l Wd,l+1 # ------- => exp(+arg dH) dU = _expm(+arg*(dH.transpose([0,2,1,3])).reshape(d*d,d*d)).reshape(d,d,d,d) # dU.setflags(write=0) # [('Wd',(l+1)),('Wd',(l+2)),('Wu',(l+1)),('Wu',(l+2))]) return dU
def add_current_label(): if cur_format == "StateVec": ar = _evalRowList( cur_rows, bComplex=True ) if ar.shape == (1,2): spam_vecs[cur_label] = _tools.state_to_pauli_density_vec(ar[0,:]) else: raise ValueError("Invalid state vector shape for %s: %s" % (cur_label,ar.shape)) elif cur_format == "DensityMx": ar = _evalRowList( cur_rows, bComplex=True ) if ar.shape == (2,2) or ar.shape == (4,4): spam_vecs[cur_label] = _tools.stdmx_to_ppvec(ar) else: raise ValueError("Invalid density matrix shape for %s: %s" % (cur_label,ar.shape)) elif cur_format == "PauliVec": spam_vecs[cur_label] = _np.transpose( _evalRowList( cur_rows, bComplex=False ) ) elif cur_format == "UnitaryMx": ar = _evalRowList( cur_rows, bComplex=True ) if ar.shape == (2,2): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_1q(ar)) elif ar.shape == (4,4): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_2q(ar)) else: raise ValueError("Invalid unitary matrix shape for %s: %s" % (cur_label,ar.shape)) elif cur_format == "UnitaryMxExp": ar = _evalRowList( cur_rows, bComplex=True ) if ar.shape == (2,2): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_1q( _expm(-1j * ar) )) elif ar.shape == (4,4): gs.gates[cur_label] = _objs.FullyParameterizedGate( _tools.unitary_to_pauligate_2q( _expm(-1j * ar) )) else: raise ValueError("Invalid unitary matrix exponent shape for %s: %s" % (cur_label,ar.shape)) elif cur_format == "PauliMx": gs.gates[cur_label] = _objs.FullyParameterizedGate( _evalRowList( cur_rows, bComplex=False ) )
def add_current(): """ Adds the current object, described by lots of cur_* variables """ qty = None if cur_format == "StateVec": ar = _evalRowList( cur_rows, bComplex=True ) if ar.shape == (1,2): stdmx = _tools.state_to_stdmx(ar[0,:]) qty = _tools.stdmx_to_vec(stdmx, basis) else: raise ValueError("Invalid state vector shape for %s: %s" % (cur_label,ar.shape)) elif cur_format == "DensityMx": ar = _evalRowList( cur_rows, bComplex=True ) if ar.shape == (2,2) or ar.shape == (4,4): qty = _tools.stdmx_to_vec(ar, basis) else: raise ValueError("Invalid density matrix shape for %s: %s" % (cur_label,ar.shape)) elif cur_format == "LiouvilleVec": qty = _np.transpose( _evalRowList( cur_rows, bComplex=False ) ) elif cur_format == "UnitaryMx": ar = _evalRowList( cur_rows, bComplex=True ) qty = _tools.change_basis(_tools.unitary_to_process_mx(ar), 'std', basis) elif cur_format == "UnitaryMxExp": ar = _evalRowList( cur_rows, bComplex=True ) qty = _tools.change_basis(_tools.unitary_to_process_mx(_expm(-1j*ar)), 'std', basis) elif cur_format == "LiouvilleMx": qty = _evalRowList( cur_rows, bComplex=False ) assert(qty is not None), "Invalid format: %s" % cur_format if cur_typ == "PREP": gs.preps[cur_label] = _objs.FullyParameterizedSPAMVec(qty) elif cur_typ == "TP-PREP": gs.preps[cur_label] = _objs.TPParameterizedSPAMVec(qty) elif cur_typ == "STATIC-PREP": gs.preps[cur_label] = _objs.StaticSPAMVec(qty) elif cur_typ in ("EFFECT","TP-EFFECT","STATIC-EFFECT"): if cur_typ == "EFFECT": qty = _objs.FullyParameterizedSPAMVec(qty) elif cur_typ == "TP-EFFECT": qty = _objs.TPParameterizedSPAMVec(qty) elif cur_typ == "STATIC-EFFECT": qty = _objs.StaticSPAMVec(qty) if "effects" in cur_group_info: cur_group_info['effects'].append( (cur_label,qty) ) else: cur_group_info['effects'] = [ (cur_label,qty) ] elif cur_typ == "GATE": gs.gates[cur_label] = _objs.FullyParameterizedGate(qty) elif cur_typ == "TP-GATE": gs.gates[cur_label] = _objs.TPParameterizedGate(qty) elif cur_typ == "CPTP-GATE": #Similar to gate.convert(...) method J = _tools.fast_jamiolkowski_iso_std(qty, basis) #Choi mx basis doesn't matter RANK_TOL = 1e-6 if _np.linalg.matrix_rank(J, RANK_TOL) == 1: unitary_post = qty # when 'gate' is unitary else: unitary_post = None nQubits = _np.log2(qty.shape[0])/2.0 bQubits = bool(abs(nQubits-round(nQubits)) < 1e-10) #integer # of qubits? proj_basis = "pp" if (basis == "pp" or bQubits) else basis ham_basis = proj_basis nonham_basis = proj_basis nonham_diagonal_only = False; cptp = True; truncate=True gs.gates[cur_label] = _objs.LindbladParameterizedGate(qty, unitary_post, ham_basis, nonham_basis, cptp, nonham_diagonal_only, truncate, basis) elif cur_typ == "STATIC-GATE": gs.gates[cur_label] = _objs.StaticGate(qty) elif cur_typ in ("IGATE","STATIC-IGATE"): mxOrGate = _objs.StaticGate(qty) if cur_typ == "STATIC-IGATE" \ else qty #just add numpy array `qty` to matrices list # and it will be made into a fully-param gate. if "matrices" in cur_group_info: cur_group_info['matrices'].append( (cur_label,mxOrGate) ) else: cur_group_info['matrices'] = [ (cur_label,mxOrGate) ] else: raise ValueError("Unknown type: %s!" % cur_typ)