def keyboard(event): global n_qubits global qubits global qubit_selected key = event.key qubit = qubits[qubit_selected] if key.isdigit(): i = int(key) if i < n_qubits: qubit_selected = i if key == "a": qubits[qubit_selected].evolve(qutip.sigmax(), inverse=True) elif key == "d": qubits[qubit_selected].evolve(qutip.sigmax(), inverse=False) elif key == "s": qubits[qubit_selected].evolve(qutip.sigmaz(), inverse=True) elif key == "w": qubits[qubit_selected].evolve(qutip.sigmaz(), inverse=False) elif key == "z": qubits[qubit_selected].evolve(qutip.sigmay(), inverse=True) elif key == "x": qubits[qubit_selected].evolve(qutip.sigmay(), inverse=False) elif key == "p": qubits[0].state = qutip.rand_herm(2) qubits[1].state = qutip.rand_herm(2)
def test_equivalent_to_matrix_element(hermitian): dimension = 20 state = qutip.rand_ket(dimension, 0.3) op = qutip.rand_herm(dimension, 0.2) if not hermitian: op = op + 1j * qutip.rand_herm(dimension, 0.1) expected = (state.dag() * op * state).data[0, 0] assert abs(qutip.expect(op, state) - expected) < 1e-14
def refresh(self, pure=False): new_state = self.parent.child_state(self) #print("&*&*") #print(self.parent.state) #print(self.parent.dims) #print(new_state) #print("23847") if self.state != None and new_state.shape[0] != self.state.shape[0]: self.precalc_bases = None self.precalc_paulis = None self.precalc_energy_eigs = None self.precalc_coherents = None self.energy = qt.rand_herm(new_state.shape[0]) if self.pure_sphere != None: self.pure_sphere.precalc_bases = None self.pure_sphere.precalc_paulis = None self.pure_sphere.precalc_energy_eigs = None self.pure_sphere.precalc_coherents = None self.pure_sphere.dimensionality = None self.dimensionality = None self.state = new_state if self.pure_sphere != None: self.pure_sphere.dt = self.dt self.pure_sphere.evolving = self.evolving self.pure_sphere.dimensionality = self.dimensionality self.pure_sphere.energy = self.energy self.pure_sphere.evolution = self.evolution if pure == False: if self.parent.is_separable(self): purevec = density_to_purevec(self.state) #print("23432") #print(purevec) #print("2345236") if self.pure_sphere == None: self.pure_sphere = PureSphere(state=purevec,\ energy=self.energy,\ dt=self.dt,\ evolving=self.evolving, evolution=self.evolution, double=self) self.pure_sphere.dimensionality = self.dimensionality else: self.pure_sphere.state = purevec self.pure_sphere.dt = self.dt self.pure_sphere.evolving = self.evolving self.pure_sphere.dimensionality = self.dimensionality self.pure_sphere.energy = self.energy self.pure_sphere.evolution = self.evolution self.pure_sphere.precalc_bases = None self.pure_sphere.precalc_paulis = None self.pure_sphere.precalc_energy_eigs = None self.pure_sphere.precalc_coherents = None else: self.pure_sphere = None if self.energy.shape[0] != self.state.shape[0]: self.energy = qt.rand_herm(self.state.shape[0]) if self.pure_sphere != None: self.pure_sphere.energy = self.energy
def testRandhermSeed(self): "random hermitian with seed" seed = 12345 U0 = rand_herm(5, seed=seed) U1 = rand_herm(5, seed=None) U2 = rand_herm(5, seed=seed) assert_(U0 != U1) assert_(U0 == U2)
def finish_up_split(self, temp_dims, temp_state): ##print("finishing up split") del self.children[0] self.dims = temp_dims self.state = temp_state self.state.dims = [self.dims, [1] * len(self.dims)] ##print("(*&*&#^$") ##print(self.state) self.children.insert( 0, MixedSphere(parent=self, energy=qt.rand_herm(self.dims[1]))) self.children.insert( 0, MixedSphere(parent=self, energy=qt.rand_herm(self.dims[0]))) self.children[0].refresh() self.children[1].refresh() return self.children[0]
def distinguishable_collapse(self, i, direction): if self.dimensionality != None: j = dim_spin(self.dimensionality[i]) op = None if direction == "x" or direction == "y" or direction == "z": op = qt.jmat(j, direction) elif direction == "r": op = qt.rand_herm(self.dimensionality[i]) total_op = op if i == 0 else qt.identity(self.dimensionality[0]) for j in range(1, len(self.dimensionality)): if j == i: total_op = qt.tensor(total_op, op) else: total_op = qt.tensor(total_op, qt.identity(self.dimensionality[j])) total_op.dims = [[self.n()], [self.n()]] L, V = total_op.eigenstates() amplitudes = [self.state.overlap(v) for v in V] probabilities = np.array([(a * np.conjugate(a)).real for a in amplitudes]) probabilities = probabilities / probabilities.sum() pick = np.random.choice(list(range(len(V))), 1, p=probabilities)[0] vec = V[pick].full().T[0] projector = qt.Qobj(np.outer(vec, np.conjugate(vec))) self.state = (projector * self.state).unit() self.refresh() return L[pick], L, probabilities
def symmetrical_collapse(self, direction, i): sym = self.symmetrical() op = None if direction == "x": op = 0.5 * qt.sigmax() elif direction == "y": op = 0.5 * qt.sigmay() elif direction == "z": op = 0.5 * qt.sigmaz() elif direction == "h": return None elif direction == "r": op = qt.rand_herm(2) total_op = op if i == 0 else qt.identity(2) for j in range(1, self.n() - 1): if j == i: total_op = qt.tensor(total_op, op) else: total_op = qt.tensor(total_op, qt.identity(2)) total_op.dims = [[(2**(self.n() - 1))], [2**(self.n() - 1)]] L, V = total_op.eigenstates() amplitudes = [sym.overlap(v) for v in V] probabilities = np.array([(a * np.conjugate(a)).real for a in amplitudes]) probabilities = probabilities / probabilities.sum() pick = np.random.choice(list(range(len(V))), 1, p=probabilities)[0] vec = V[pick].full().T[0] projector = qt.Qobj(np.outer(vec, np.conjugate(vec))) sym = (projector * sym).unit() self.state = unsymmeterize(sym) self.refresh() return L[pick], L, probabilities
def destroy_star(self): if self.n() > 2: self.state = SurfaceXYZ_q(self.stars()[1:]).unit() self.energy = qt.rand_herm(self.n()) self.refresh(dimchange=True) self.dimensionality = None return self.state
def create_star(self): xyz = q_SurfaceXYZ(qt.rand_ket(2)) self.state = SurfaceXYZ_q(self.stars() + xyz).unit() self.energy = qt.rand_herm(self.n()) self.refresh(dimchange=True) self.dimensionality = None return self.state
def __init__(self, state=None,\ energy=None,\ dt=0.01,\ evolving=False, evolution="spin",\ double=None): self.state = state if state != None else qt.rand_ket(2) self.energy = energy if energy != None else qt.rand_herm(self.n()) self.dt = dt self.evolving = evolving self.evolution = evolution self.dimensionality = None self.double = double self.precalc_stars = None self.precalc_plane_stars = None self.precalc_component_stars = None self.precalc_plane_component_stars = None self.precalc_polynomial = None self.precalc_qubits = None self.precalc_symmetrical = None self.precalc_bases = None self.precalc_paulis = None self.precalc_energy_eigs = None self.precalc_coherents = None self.precalc_1Dfock = None self.precalc_1Dops = None self.precalc_2Dfock = None self.precalc_2Dops = None self.precalc_1Dstuff = None self.precalc_2Dstuff = None self.dim_change = False
def test_DenseValsOnly(): """ Dense eigvals only Hermitian """ H = rand_herm(10) spvals = H.eigenenergies(sparse=False) assert_equal(len(spvals), 10) # check that sorting is lowest eigval first assert_equal(spvals[0] <= spvals[-1], True) # check that spvals equal expect vals for k in range(10): # check that ouput is real for Hermitian operator assert_equal(np.isreal(spvals[k]), True) spvals = H.eigenenergies(sparse=False, sort='high') # check that sorting is lowest eigval first assert_equal(spvals[0] >= spvals[-1], True) spvals = H.eigenenergies(sparse=False, sort='high', eigvals=4) assert_equal(len(spvals), 4) U = rand_unitary(10) spvals = U.eigenenergies(sparse=False) assert_equal(len(spvals), 10) # check that sorting is lowest eigval first assert_equal(spvals[0] <= spvals[-1], True) # check that spvals equal expect vals for k in range(10): # check that ouput is real for Hermitian operator assert_equal(np.iscomplex(spvals[k]), True) spvals = U.eigenenergies(sparse=False, sort='high') # check that sorting is lowest eigval first assert_equal(spvals[0] >= spvals[-1], True) spvals = U.eigenenergies(sparse=False, sort='high', eigvals=4) assert_equal(len(spvals), 4)
def keyboard(self, event): key = event.key spin_ops = self.spin_operators() if key == "a": self.evolve(spin_ops['X'], inverse=True, dt=0.01) elif key == "d": self.evolve(spin_ops['X'], inverse=False, dt=0.01) elif key == "s": self.evolve(spin_ops['Z'], inverse=True, dt=0.01) elif key == "w": self.evolve(spin_ops['Z'], inverse=False, dt=0.01) elif key == "z": self.evolve(spin_ops['Y'], inverse=True, dt=0.01) elif key == "x": self.evolve(spin_ops['Y'], inverse=False, dt=0.01) elif key == "e": if self.evolving: self.evolving = False else: self.evolving = True elif key == "r": self.state = qt.rand_ket(self.n) elif key == "t": self.energy = qt.rand_herm(self.n) elif key == "y": a = qt.destroy(modes) n = qt.num(modes) x = (a + a.dag()) / np.sqrt(2) p = -1j * (a - a.dag()) / np.sqrt(2) H = 2 * np.pi * freq * a.dag() * a self.energy = H elif key == "[": self.dt += 0.001 elif key == "]": self.dt -= 0.001
def test_vector_roundtrip(): "BR Tools : vector roundtrip transform" dimension = 10 for _ in range(50): H = qutip.rand_herm(dimension, 0.5).full('F') vector = qutip.mat2vec(qutip.rand_dm(dimension, 0.5).full()).ravel() assert np.allclose(vector, _test_vector_roundtrip(H, vector))
def __init__(self, n_qubits, center=vpython.vector(0, 0, 0), radius=1.0, color=vpython.color.blue): self.n_qubits = n_qubits self.center = center self.radius = radius self.color = color self.state = qutip.rand_ket(2**self.n_qubits) self.energy = qutip.rand_herm(2**self.n_qubits) self.vsphere = vpython.sphere(pos=self.center,\ radius=self.radius,\ color=self.color,\ opacity=0.3) qubit_colors = [vpython.vector(random.random(), random.random(), random.random())\ for i in range(self.n_qubits)] self.qubits = [ Qubit(color=qubit_colors[i], parent=self, gauged=True) for i in range(self.n_qubits) ] self.update()
def test_13_krylovsolve_bad_krylov_dim(dim=15, krylov_dim=20): """Check errors from bad krylov dimension inputs.""" H = rand_herm(dim) psi0 = basis(dim, 0) tlist = np.linspace(0, 1, 100) with pytest.raises(ValueError) as exc: krylovsolve(H, psi0, tlist, krylov_dim)
def test_Transformation8(): "Check Qobj eigs and direct eig solver reverse transformations match" N = 10 H = rand_herm(N) # generate a random basis rand = rand_dm(N, density=1) evals, rand_basis = rand.eigenstates() evals2, rand_basis2 = sp_eigs(rand.data, isherm=1) H1 = H.transform(rand_basis, True) H2 = H.transform(rand_basis2, True) assert_((H1 - H2).norm() < 1e-6) ket = rand_ket(N) K1 = ket.transform(rand_basis,1) K2 = ket.transform(rand_basis2,1) assert_((K1 - K2).norm() < 1e-6) bra = rand_ket(N).dag() B1 = bra.transform(rand_basis,1) B2 = bra.transform(rand_basis2,1) assert_((B1 - B2).norm() < 1e-6)
def random_sphere(dimensionality): n = collapser(dimensionality) pure_state = qutip.rand_ket(n) state = pure_state.ptrace(0) #state = qutip.rand_herm(n) energy = qutip.rand_herm(n) return Sphere(state=state, dimensionality=dimensionality, energy=energy)
def __init__(self, state=None,\ center=vpython.vector(0,0,0), radius=1, color=vpython.color.blue, star_color=vpython.color.white): if state == None: self.state = qutip.rand_herm(2) else: self.state = state self.center = center self.radius = radius if color == None: self.color = vpython.vector(random.random(),\ random.random(),\ random.random()) else: self.color = color self.star_color = star_color self.vsphere = vpython.sphere(pos=self.center,\ radius=self.radius,\ color=self.color,\ opacity=0.4) self.varrow = vpython.arrow(pos=self.center,\ color=self.color,\ shaftwidth=0.05,\ emissive=True) self.vbase = vpython.sphere(color=self.star_color,\ radius=0.1*self.radius,\ opacity=0.7,\ emissive=True)
def testRandhermEigs(self): "Random: Hermitian - Eigs given" H = [rand_herm([1, 2, 3, 4, 5], 0.5) for k in range(5)] for h in H: eigs = sp_eigs(h.data, h.isherm, vecs=False) assert_(np.abs(np.sum(eigs) - 15.0) < 1e-12)
def keyboard(e): global n, state, XP, XL, YP, YL, ZP, ZL, NP, NL, QP, QL, H, U, X, Y, Z k = e.key if k == "x": probs = np.array([qt.expect(XP[i], state) for i in range(n * n)]) print(probs) choice = np.random.choice(list(range(n * n)), p=abs(probs / sum(probs))) state = (XP[choice] * state).unit() print(XL[choice]) elif k == "y": probs = np.array([qt.expect(YP[i], state) for i in range(n * n)]) print(probs) choice = np.random.choice(list(range(n * n)), p=abs(probs / sum(probs))) state = (YP[choice] * state).unit() print(YL[choice]) elif k == "z": probs = np.array([qt.expect(ZP[i], state) for i in range(n * n)]) print(probs) choice = np.random.choice(list(range(n * n)), p=abs(probs / sum(probs))) state = (ZP[choice] * state).unit() print(ZL[choice]) elif k == "n": probs = np.array([qt.expect(NP[i], state) for i in range(n * n)]) print(probs) choice = np.random.choice(list(range(n * n)), p=abs(probs / sum(probs))) state = (NP[choice] * state).unit() print(NL[choice]) elif k == "i": state = qt.rand_ket(n * n) state.dims = [[n, n], [1, 1]] elif k == "q": probs = [] indices = [] for i in range(n): for j in range(n): probs.append(qt.expect(Qprojs[i][j], state)) indices.append((i, j)) probs = np.array(probs) print(probs) choice = np.random.choice(list(range(n * n)), p=abs(probs / sum(probs))) i, j = indices[choice] state = (Qprojs[i][j] * state).unit() print(QL[i], QL[j]) elif k == "e": H = N + 1 #X#qt.rand_herm(n*n) H.dims = [[n, n], [n, n]] U = (-1j * H * dt).expm() elif k == "h": H = qt.rand_herm(n * n) H.dims = [[n, n], [n, n]] U = (-1j * H * dt).expm() elif k == "s": H = [X, Y, Z][np.random.randint(3)] H.dims = [[n, n], [n, n]] U = (-1j * H * dt).expm()
def testRandhermEigs(self): "Random: Hermitian - Eigs given" H = [rand_herm([1,2,3,4,5],0.5) for k in range(5)] for h in H: eigs = sp_eigs(h.data, h.isherm, vecs=False) assert_(np.abs(np.sum(eigs)-15.0) < 1e-12)
def test_Transformation8(): "Check Qobj eigs and direct eig solver reverse transformations match" N = 10 H = rand_herm(N) # generate a random basis rand = rand_dm(N, density=1) evals, rand_basis = rand.eigenstates() evals2, rand_basis2 = sp_eigs(rand.data, isherm=1) H1 = H.transform(rand_basis, True) H2 = H.transform(rand_basis2, True) assert_((H1 - H2).norm() < 1e-6) ket = rand_ket(N) K1 = ket.transform(rand_basis, 1) K2 = ket.transform(rand_basis2, 1) assert_((K1 - K2).norm() < 1e-6) bra = rand_ket(N).dag() B1 = bra.transform(rand_basis, 1) B2 = bra.transform(rand_basis2, 1) assert_((B1 - B2).norm() < 1e-6)
def test_07_check_e_ops_list_single_callable(self, e_ops_type, dimensions, tlists): "krylovsolve: check e_ops=[call | qobj] random H different tlists." psi0 = rand_ket(dimensions) H = rand_herm(dimensions, density=0.5) e_ops = create_test_e_ops(e_ops_type, dimensions) self.check_e_ops_list_single_operator( e_ops, H, psi0, tlists, dimensions)
def test_08_check_e_ops_mixed_list(self, e_ops_type, dimensions, tlists): "krylovsolve: check e_ops=[call | qobj] with len(e_ops)>1 for" "random H different tlists." psi0 = rand_ket(dimensions) H = rand_herm(dimensions, density=0.5) e_ops = create_test_e_ops(e_ops_type, dimensions) self.check_e_ops_mixed_list(e_ops, H, psi0, tlists, dimensions)
def test_02_simple_check_states_e_ops_H_random(self): "krylovsolve: states with const H random" dim = 512 psi0 = rand_ket(dim) H = rand_herm(dim) tlist = np.linspace(0, 10, 200) self.simple_check_states_e_ops(H, psi0, tlist)
def test_symmetric_solver(self, dtype): A = qutip.rand_herm(np.arange(1, 11)).data if dtype == np.float64: A = A.real x = np.ones(10, dtype=dtype) b = A.dot(x) y = mkl_spsolve(A, b, hermitian=1, verbose=True) np.testing.assert_allclose(x, y)
def test_rand_herm(density, pos_def): """ Random Qobjs: Hermitian matrix """ random_qobj = rand_herm(5, density=density, pos_def=pos_def) if pos_def: assert all(random_qobj.eigenenergies() > -1e14) assert random_qobj.isherm
def evolve(self, H=None, dt=0.01, T=100): if type(H) == type(None): H = qt.rand_herm(self.nfaces) U = (-1j * H * dt).expm().full() for t in range(T): self.spinors = [sum([U[i][j]*self.spinors[j]\ for j in range(self.nfaces)])\ for i in range(self.nfaces)] self.poly = polyhedrec.reconstruct(self.normals(), self.areas()) self.vpoly.update(self.poly) if self.show_poly else None
def evolve(self, H=None, T=1000, dt=0.005): if type(H) == type(None): H = qt.rand_herm(self.d) U = (-1j * H * dt).expm() for i in range(T): if self.spin.type == "ket": self.spin = U * self.spin else: self.spin = U * self.spin * U.dag() self.update_viz()
def recreate(): global n_qubits global state global energy global unitary global qubits global qubit_colors global vsphere global vstars global vbases global varrows global vfibers global vspin state = qutip.rand_ket(2**n_qubits) energy = qutip.rand_herm(2**n_qubits) unitary = qutip.Qobj( scipy.linalg.expm(-2 * math.pi * im * energy.full() * dt)) qubits = None qubit_colors = [ vpython.vector(random.random(), random.random(), random.random()) for i in range(n_qubits) ] vsphere.visible = False del vsphere vsphere = vpython.sphere(pos=vpython.vector(0,0,0),\ radius=1.0,\ color=vpython.color.blue,\ opacity=0.4) vspin.visible = False del vspin vspin = vpython.arrow(shaftwidth=0.01, headwidth=0.001, headlength=0.001) for vstar in vstars: vstar.visible = False del vstar vstars = [vpython.sphere(radius=0.05,\ color=vpython.color.white,\ opacity=0.8,\ emissive=True) for i in range((2**n_qubits)-1)] for vbase in vbases: vbase.visible = False del vbase vbases = [vpython.sphere(radius=0.1,\ color=qubit_colors[i],\ opacity=0.7,\ emissive=True,\ make_trail=False) for i in range(n_qubits)] for varrow in varrows: varrow.visible = False del varrow varrows = [vpython.arrow(color=qubit_colors[i]) for i in range(n_qubits)] for vfiber in vfibers: vfiber.visible = False del vfiber vfibers = [vpython.curve(pos=[vpython.vector(0,0,0) for i in range(n_points)],\ color=qubit_colors[i]) for i in range(n_qubits)]
def test_DenseValsVecs(): """ Dense eigs non-Hermitian """ W = rand_herm(10,0.5) + 1j*rand_herm(10,0.5) spvals, spvecs = W.eigenstates(sparse=False) assert_equal(np.real(spvals[0]) <= np.real(spvals[-1]), True) for k in range(10): # check that eigenvectors are right and in right order assert_equal(abs(expect(W, spvecs[k]) - spvals[k]) < 1e-14, True) assert_(np.iscomplex(spvals[k])) # check sorting spvals, spvecs = W.eigenstates(sparse=False, sort='high') assert_equal(np.real(spvals[0]) >= np.real(spvals[-1]), True) # check for N-1 eigenvals W = rand_unitary(10) spvals, spvecs = W.eigenstates(sparse=False, eigvals=9) assert_equal(len(spvals), 9)
def test_vec_to_eigbasis(): "BR Tools : vector to eigenbasis" dimension = 10 for _ in range(50): H = qutip.rand_herm(dimension, 0.5) basis = H.eigenstates()[1] R = qutip.rand_dm(dimension, 0.5) target = qutip.mat2vec(R.transform(basis).full()).ravel() flat_vector = qutip.mat2vec(R.full()).ravel() calculated = _test_vec_to_eigbasis(H.full('F'), flat_vector) np.testing.assert_allclose(target, calculated, atol=1e-12)
def test_setTDFormatCheckMC(): "td_format_check: monte-carlo" # define operators H = rand_herm(10) c_op = qeye(10) def f_c_op(t, args): return 0 def f_H(t, args): return 0 # check constant H and no C_ops time_type, h_stuff, c_stuff = _td_format_check(H, [], 'mc') assert_(time_type == 0) # check constant H and constant C_ops time_type, h_stuff, c_stuff = _td_format_check(H, [c_op], 'mc') assert_(time_type == 0) # check constant H and str C_ops time_type, h_stuff, c_stuff = _td_format_check(H, [c_op, '1'], 'mc') # assert_(time_type==1) # this test fails!! # check constant H and func C_ops time_type, h_stuff, c_stuff = _td_format_check(H, [f_c_op], 'mc') # assert_(time_type==2) # FAILURE # check str H and constant C_ops time_type, h_stuff, c_stuff = _td_format_check([H, '1'], [c_op], 'mc') # assert_(time_type==10) # check str H and str C_ops time_type, h_stuff, c_stuff = _td_format_check([H, '1'], [c_op, '1'], 'mc') # assert_(time_type==11) # check str H and func C_ops time_type, h_stuff, c_stuff = _td_format_check([H, '1'], [f_c_op], 'mc') # assert_(time_type==12) # check func H and constant C_ops time_type, h_stuff, c_stuff = _td_format_check(f_H, [c_op], 'mc') # assert_(time_type==20) # check func H and str C_ops time_type, h_stuff, c_stuff = _td_format_check(f_H, [c_op, '1'], 'mc') # assert_(time_type==21) # check func H and func C_ops time_type, h_stuff, c_stuff = _td_format_check(f_H, [f_c_op], 'mc')
def test_SparseHermValsVecs(): """ Sparse eigs Hermitian """ # check using number operator N = num(10) spvals, spvecs = N.eigenstates(sparse=True) for k in range(10): # check that eigvals are in proper order assert_equal(abs(spvals[k] - k) <= 1e-13, True) # check that eigenvectors are right and in right order assert_equal(abs(expect(N, spvecs[k]) - spvals[k]) < 5e-14, True) # check ouput of only a few eigenvals/vecs spvals, spvecs = N.eigenstates(sparse=True, eigvals=7) assert_equal(len(spvals), 7) assert_equal(spvals[0] <= spvals[-1], True) for k in range(7): assert_equal(abs(spvals[k] - k) < 1e-12, True) spvals, spvecs = N.eigenstates(sparse=True, sort='high', eigvals=5) assert_equal(len(spvals), 5) assert_equal(spvals[0] >= spvals[-1], True) vals = np.arange(9, 4, -1) for k in range(5): # check that eigvals are ordered from high to low assert_equal(abs(spvals[k] - vals[k]) < 5e-14, True) assert_equal(abs(expect(N, spvecs[k]) - vals[k]) < 1e-14, True) # check using random Hermitian H = rand_herm(10) spvals, spvecs = H.eigenstates(sparse=True) # check that sorting is lowest eigval first assert_equal(spvals[0] <= spvals[-1], True) # check that spvals equal expect vals for k in range(10): assert_equal(abs(expect(H, spvecs[k]) - spvals[k]) < 5e-14, True) # check that ouput is real for Hermitian operator assert_equal(np.isreal(spvals[k]), True)
import os import qutip import sys import timeit if len(sys.argv) != 3: sys.exit("Please specify the number of cores and qubits!") num_cores = int(sys.argv[1]) # number of cores n = int(sys.argv[2]) # number of qubits N = 2 ** n os.environ['OPENBLAS_NUM_THREADS'] = str(num_cores) os.environ['MKL_NUM_THREADS'] = str(num_cores) qutip.settings.num_cpus = num_cores result = qutip.rand_herm(N, dims=[[2] * n, [2] * n]) # start timing start_time = timeit.default_timer() # partial trace over the first qubit result = result.ptrace(range(1, n)) elapsed = timeit.default_timer() - start_time # end timing print("{0}, {1}, {2}".format(num_cores, n, elapsed))
def testRandherm(self): "random hermitian" H = [rand_herm(5) for k in range(5)] for h in H: assert_equal(h.isherm, True)
def testRandhermPosDef(self): "Random: Hermitian - Positive semi-def" H = [rand_herm(5,pos_def=1) for k in range(5)] for h in H: assert_(not any(sp_eigs(h.data, h.isherm, vecs=False)) < 0)
import numpy as np import qutip as qt import h5py as h5 num_samples = 100000 dimensions = 100 f = h5.File('eigdata.hdf5','w') matset = f.create_dataset("matrices", (num_samples, 2*(dimensions**2)), dtype='f') eigvalset = f.create_dataset("eigvals", (num_samples, 1), dtype='f') eigvecset = f.create_dataset("eigvecs", (num_samples, 2*dimensions), dtype='f') for i in np.arange(num_samples): print 'generating samples %d' %(i+1) mat = qt.rand_herm(dimensions) eigval, eigvec = mat.groundstate() mat = np.reshape(mat.full(), (dimensions**2,)) real_mat = np.real(mat) imag_mat = np.imag(mat) eigvec = np.reshape(eigvec.full(), (dimensions,)) real_eigvec = np.real(eigvec) imag_eigvec = np.imag(eigvec) matset[i][...] = np.append(real_mat, imag_mat) eigvalset[i] = eigval eigvecset[i][...] = np.append(real_eigvec, imag_eigvec) f.close()