def test_hamiltonian_matmul(): """Test matrix multiplication between Hamiltonians and state vectors.""" H1 = hamiltonians.TFIM(nqubits=3, h=1.0) H2 = hamiltonians.Y(nqubits=3) m1 = K.to_numpy(H1.matrix) m2 = K.to_numpy(H2.matrix) K.assert_allclose((H1 @ H2).matrix, m1 @ m2) K.assert_allclose((H2 @ H1).matrix, m2 @ m1) v = random_complex(8, dtype=m1.dtype) m = random_complex((8, 8), dtype=m1.dtype) H1v = H1 @ K.cast(v) H1m = H1 @ K.cast(m) K.assert_allclose(H1v, m1.dot(v)) K.assert_allclose(H1m, m1 @ m) from qibo.core.states import VectorState H1state = H1 @ VectorState.from_tensor(K.cast(v)) K.assert_allclose(H1state, m1.dot(v)) with pytest.raises(ValueError): H1 @ np.zeros((8, 8, 8), dtype=m1.dtype) with pytest.raises(NotImplementedError): H1 @ 2
def test_symbolic_hamiltonian_abstract_symbol_ev(backend, density_matrix, calcterms): from qibo.symbols import X, Symbol matrix = np.random.random((2, 2)) form = X(0) * Symbol(1, matrix) + Symbol(0, matrix) * X(1) local_ham = hamiltonians.SymbolicHamiltonian(form) if calcterms: _ = local_ham.terms if density_matrix: state = K.cast(random_complex((4, 4))) else: state = K.cast(random_complex((4, ))) local_ev = local_ham.expectation(state) target_ev = local_ham.dense.expectation(state) K.assert_allclose(local_ev, target_ev)
def test_hamiltonian_expectation_errors(): h = hamiltonians.XXZ(nqubits=3, delta=0.5) state = random_complex((4, 4, 4)) with pytest.raises(ValueError): h.expectation(state) with pytest.raises(TypeError): h.expectation("test")
def test_hamiltonian_expectation(dense, density_matrix): h = hamiltonians.XXZ(nqubits=3, delta=0.5, dense=dense) matrix = K.to_numpy(h.matrix) if density_matrix: state = random_complex((8, 8)) state = state + state.T.conj() norm = np.trace(state) target_ev = np.trace(matrix.dot(state)).real else: state = random_complex(8) norm = np.sum(np.abs(state)**2) target_ev = np.sum(state.conj() * matrix.dot(state)).real K.assert_allclose(h.expectation(state), target_ev) K.assert_allclose(h.expectation(state, True), target_ev / norm)
def test_symbolic_hamiltonian_matmul(backend, nqubits, density_matrix, calcterms): if density_matrix: from qibo.core.states import MatrixState shape = (2**nqubits, 2**nqubits) state = MatrixState.from_tensor(random_complex(shape)) else: from qibo.core.states import VectorState shape = (2**nqubits, ) state = VectorState.from_tensor(random_complex(shape)) local_ham = hamiltonians.SymbolicHamiltonian(symbolic_tfim(nqubits, h=1.0)) dense_ham = hamiltonians.TFIM(nqubits, h=1.0) if calcterms: _ = local_ham.terms local_matmul = local_ham @ state target_matmul = dense_ham @ state K.assert_allclose(local_matmul, target_matmul)
def test_symbolic_hamiltonian_state_ev(backend, nqubits, normalize, calcterms, calcdense): local_ham = hamiltonians.SymbolicHamiltonian(symbolic_tfim(nqubits, h=1.0)) + 2 if calcterms: _ = local_ham.terms if calcdense: _ = local_ham.dense dense_ham = hamiltonians.TFIM(nqubits, h=1.0) + 2 state = K.cast(random_complex((2**nqubits, ))) local_ev = local_ham.expectation(state, normalize) target_ev = dense_ham.expectation(state, normalize) K.assert_allclose(local_ev, target_ev) state = random_complex((2**nqubits, )) local_ev = local_ham.expectation(state, normalize) target_ev = dense_ham.expectation(state, normalize) K.assert_allclose(local_ev, target_ev)
def test_trotter_hamiltonian_matmul(nqubits, normalize): """Test Trotter Hamiltonian expectation value.""" local_ham = hamiltonians.TFIM(nqubits, h=1.0, dense=False) dense_ham = hamiltonians.TFIM(nqubits, h=1.0) state = K.cast(random_complex((2 ** nqubits,))) trotter_ev = local_ham.expectation(state, normalize) target_ev = dense_ham.expectation(state, normalize) K.assert_allclose(trotter_ev, target_ev) state = random_complex((2 ** nqubits,)) trotter_ev = local_ham.expectation(state, normalize) target_ev = dense_ham.expectation(state, normalize) K.assert_allclose(trotter_ev, target_ev) from qibo.core.states import VectorState state = VectorState.from_tensor(state) trotter_matmul = local_ham @ state target_matmul = dense_ham @ state K.assert_allclose(trotter_matmul, target_matmul)
def test_hamiltonian_expectation(backend, dense, density_matrix, sparse_type): """Test Hamiltonian expectation value calculation.""" if sparse_type is None: h = hamiltonians.XXZ(nqubits=3, delta=0.5, dense=dense) else: h = hamiltonians.Hamiltonian(6, random_sparse_matrix(64, sparse_type)) matrix = K.to_numpy(h.matrix) if density_matrix: state = random_complex((2 ** h.nqubits, 2 ** h.nqubits)) state = state + state.T.conj() norm = np.trace(state) target_ev = np.trace(matrix.dot(state)).real else: state = random_complex(2 ** h.nqubits) norm = np.sum(np.abs(state) ** 2) target_ev = np.sum(state.conj() * matrix.dot(state)).real K.assert_allclose(h.expectation(state), target_ev) K.assert_allclose(h.expectation(state, True), target_ev / norm)
def test_hamiltonian_matmul_states(backend, sparse_type): """Test matrix multiplication between Hamiltonian and states.""" if sparse_type is None: nqubits = 3 H = hamiltonians.TFIM(nqubits, h=1.0) else: nqubits = 5 nstates = 2 ** nqubits H = hamiltonians.Hamiltonian(nqubits, random_sparse_matrix(nstates, sparse_type)) hm = K.to_numpy(H.matrix) v = random_complex(2 ** nqubits, dtype=hm.dtype) m = random_complex((2 ** nqubits, 2 ** nqubits), dtype=hm.dtype) Hv = H @ K.cast(v) Hm = H @ K.cast(m) K.assert_allclose(Hv, hm.dot(v)) K.assert_allclose(Hm, hm @ m) from qibo.core.states import VectorState Hstate = H @ VectorState.from_tensor(K.cast(v)) K.assert_allclose(Hstate, hm.dot(v))