def test_hamiltonian_eigenvectors(dtype, numpy, trotter): """Testing hamiltonian eigenvectors scaling.""" H1 = XXZ(nqubits=2, delta=0.5, numpy=numpy, trotter=trotter) V1 = np.array(H1.eigenvectors()) U1 = np.array(H1.eigenvalues()) np.testing.assert_allclose(H1.matrix, V1 @ np.diag(U1) @ V1.T) # Check ground state np.testing.assert_allclose(H1.ground_state(), V1[:, 0]) c1 = dtype(2.5) H2 = c1 * H1 V2 = np.array(H2._eigenvectors) U2 = np.array(H2._eigenvalues) np.testing.assert_allclose(H2.matrix, V2 @ np.diag(U2) @ V2.T) c2 = dtype(-11.1) H3 = H1 * c2 V3 = np.array(H3.eigenvectors()) U3 = np.array(H3._eigenvalues) np.testing.assert_allclose(H3.matrix, V3 @ np.diag(U3) @ V3.T) c3 = dtype(0) H4 = c3 * H1 V4 = np.array(H4._eigenvectors) U4 = np.array(H4._eigenvalues) np.testing.assert_allclose(H4.matrix, V4 @ np.diag(U4) @ V4.T)
def test_hamiltonian_runtime_errors(numpy): """Testing hamiltonian runtime errors.""" H1 = XXZ(nqubits=2, delta=0.5, numpy=numpy) H2 = XXZ(nqubits=3, delta=0.1, numpy=numpy) with pytest.raises(RuntimeError): R = H1 + H2 with pytest.raises(RuntimeError): R = H1 - H2
def test_hamiltonian_expectation(numpy, trotter): h = XXZ(nqubits=3, delta=0.5, numpy=numpy, trotter=trotter) matrix = np.array(h.matrix) state = utils.random_numpy_complex(8) norm = (np.abs(state)**2).sum() target_ev = (state.conj() * matrix.dot(state)).sum().real np.testing.assert_allclose(h.expectation(state), target_ev) np.testing.assert_allclose(h.expectation(state, True), target_ev / norm)
def test_hamiltonian_notimplemented_errors(numpy): """Testing hamiltonian not implemented errors.""" H1 = XXZ(nqubits=2, delta=0.5, numpy=numpy) H2 = XXZ(nqubits=2, delta=0.1, numpy=numpy) with pytest.raises(NotImplementedError): R = H1 * H2 with pytest.raises(NotImplementedError): R = H1 + "a" with pytest.raises(NotImplementedError): R = H2 - (2, ) with pytest.raises(NotImplementedError): R = [3] - H1
def test_hamiltonian_expectation(numpy, trotter, density_matrix): h = XXZ(nqubits=3, delta=0.5, numpy=numpy, trotter=trotter) matrix = np.array(h.matrix) if density_matrix: state = utils.random_numpy_complex((8, 8)) state = state + state.T.conj() norm = np.trace(state) target_ev = np.trace(matrix.dot(state)).real else: state = utils.random_numpy_complex(8) norm = np.sum(np.abs(state)**2) target_ev = np.sum(state.conj() * matrix.dot(state)).real np.testing.assert_allclose(h.expectation(state), target_ev) np.testing.assert_allclose(h.expectation(state, True), target_ev / norm)
def test_vqe_custom_gates_errors(): """Check that ``RuntimeError``s is raised when using custom gates.""" import qibo original_backend = qibo.get_backend() qibo.set_backend("custom") nqubits = 6 circuit = Circuit(nqubits) for q in range(nqubits): circuit.add(gates.RY(q, theta=0)) for q in range(0, nqubits - 1, 2): circuit.add(gates.CZ(q, q + 1)) hamiltonian = XXZ(nqubits=nqubits) initial_parameters = np.random.uniform(0, 2 * np.pi, 2 * nqubits + nqubits) v = VQE(circuit, hamiltonian) # compile with custom gates with pytest.raises(RuntimeError): best, params = v.minimize(initial_parameters, method="BFGS", options={'maxiter': 1}, compile=True) # use SGD with custom gates with pytest.raises(RuntimeError): best, params = v.minimize(initial_parameters, method="sgd", compile=False) qibo.set_backend(original_backend)
def test_hamiltonian_eigenvalues(dtype, numpy, trotter): """Testing hamiltonian eigenvalues scaling.""" H1 = XXZ(nqubits=2, delta=0.5, numpy=numpy, trotter=trotter) H1_eigen = H1.eigenvalues() hH1_eigen = np.linalg.eigvalsh(H1.matrix) np.testing.assert_allclose(H1_eigen, hH1_eigen) c1 = dtype(2.5) H2 = c1 * H1 hH2_eigen = np.linalg.eigvalsh(c1 * H1.matrix) np.testing.assert_allclose(H2._eigenvalues, hH2_eigen) c2 = dtype(-11.1) H3 = H1 * c2 hH3_eigen = np.linalg.eigvalsh(H1.matrix * c2) np.testing.assert_allclose(H3._eigenvalues, hH3_eigen)
def test_hamiltonian_overloading(dtype, numpy): """Test basic hamiltonian overloading.""" def transformation_a(a, b): c1 = dtype(0.1) return a + c1 * b def transformation_b(a, b): c1 = dtype(2) c2 = dtype(3.5) return c1 * a - b * c2 def transformation_c(a, b, use_eye=False): c1 = dtype(4.5) if use_eye: return a + c1 * np.eye(a.shape[0]) - b else: return a + c1 - b def transformation_d(a, b, use_eye=False): c1 = dtype(10.5) c2 = dtype(2) if use_eye: return c1 * np.eye(a.shape[0]) - a + c2 * b else: return c1 - a + c2 * b H1 = XXZ(nqubits=2, delta=0.5, numpy=numpy) H2 = XXZ(nqubits=2, delta=1, numpy=numpy) hH1 = transformation_a(H1.matrix, H2.matrix) hH2 = transformation_b(H1.matrix, H2.matrix) hH3 = transformation_c(H1.matrix, H2.matrix, use_eye=True) hH4 = transformation_d(H1.matrix, H2.matrix, use_eye=True) HT1 = transformation_a(H1, H2) HT2 = transformation_b(H1, H2) HT3 = transformation_c(H1, H2) HT4 = transformation_d(H1, H2) np.testing.assert_allclose(hH1, HT1.matrix) np.testing.assert_allclose(hH2, HT2.matrix) np.testing.assert_allclose(hH3, HT3.matrix) np.testing.assert_allclose(hH4, HT4.matrix)
def test_hamiltonian_exponentiation(numpy, trotter): from scipy.linalg import expm H = XXZ(nqubits=2, delta=0.5, numpy=numpy, trotter=trotter) target_matrix = expm(-0.5j * np.array(H.matrix)) np.testing.assert_allclose(H.exp(0.5), target_matrix) H = XXZ(nqubits=2, delta=0.5, numpy=numpy, trotter=trotter) _ = H.eigenvectors() np.testing.assert_allclose(H.exp(0.5), target_matrix)
def test_trotter_hamiltonian_make_compatible(nqubits): """Test that ``make_compatible`` method works for ``X`` Hamiltonian.""" h0target = X(nqubits) h0 = X(nqubits, trotter=True) h1 = XXZ(nqubits, delta=0.5, trotter=True) assert not h1.is_compatible(h0) assert not h0.is_compatible(h1) np.testing.assert_allclose(h0.matrix, h0target.matrix) h0c = h1.make_compatible(h0) assert not h1.is_compatible(h0) assert h1.is_compatible(h0c) assert h0c.is_compatible(h1) np.testing.assert_allclose(h0.matrix, h0target.matrix) np.testing.assert_allclose(h0c.matrix, h0target.matrix) # for coverage h0c = h1.make_compatible(h0c) assert not h1.is_compatible("test") h2 = XXZ(nqubits, delta=0.5, trotter=True) h2.parts[0].pop((0, 1)) assert not h1.is_compatible(h2)
def test_vector_state_expectation(backend, dense): from qibo.hamiltonians import XXZ ham = XXZ(nqubits=5, delta=0.5, dense=dense) matrix = K.to_numpy(ham.matrix) state = np.random.random(32) + 1j * np.random.random(32) norm = np.sum(np.abs(state)**2) target_ev = np.sum(state.conj() * matrix.dot(state)).real state = states.VectorState.from_tensor(state) K.assert_allclose(state.expectation(ham), target_ev) K.assert_allclose(state.expectation(ham, True), target_ev / norm)
def test_hamiltonian_expectation_errors(): h = XXZ(nqubits=3, delta=0.5) state = utils.random_numpy_complex((4, 4, 4)) with pytest.raises(ValueError): h.expectation(state) with pytest.raises(TypeError): h.expectation("test")
def test_vqe(method, options, compile, filename): """Performs a VQE circuit minimization test.""" import qibo original_backend = qibo.get_backend() if method == "sgd" or compile: qibo.set_backend("matmuleinsum") else: qibo.set_backend("custom") original_threads = get_threads() if method == 'parallel_L-BFGS-B': if 'GPU' in qibo.get_device(): # pragma: no cover pytest.skip("unsupported configuration") qibo.set_threads(1) nqubits = 6 layers = 4 circuit = Circuit(nqubits) for l in range(layers): for q in range(nqubits): circuit.add(gates.RY(q, theta=1.0)) for q in range(0, nqubits - 1, 2): circuit.add(gates.CZ(q, q + 1)) for q in range(nqubits): circuit.add(gates.RY(q, theta=1.0)) for q in range(1, nqubits - 2, 2): circuit.add(gates.CZ(q, q + 1)) circuit.add(gates.CZ(0, nqubits - 1)) for q in range(nqubits): circuit.add(gates.RY(q, theta=1.0)) hamiltonian = XXZ(nqubits=nqubits) np.random.seed(0) initial_parameters = np.random.uniform(0, 2 * np.pi, 2 * nqubits * layers + nqubits) v = VQE(circuit, hamiltonian) best, params = v.minimize(initial_parameters, method=method, options=options, compile=compile) if method == "cma": # remove `outcmaes` folder import shutil shutil.rmtree("outcmaes") if filename is not None: utils.assert_regression_fixture(params, filename) qibo.set_backend(original_backend) qibo.set_threads(original_threads)
def test_vector_state_expectation(backend, trotter): original_backend = qibo.get_backend() qibo.set_backend(backend) from qibo.hamiltonians import XXZ ham = XXZ(nqubits=5, delta=0.5, trotter=trotter) matrix = np.array(ham.matrix) state = np.random.random(32) + 1j * np.random.random(32) norm = np.sum(np.abs(state)**2) target_ev = np.sum(state.conj() * matrix.dot(state)).real state = states.VectorState.from_tensor(state) np.testing.assert_allclose(state.expectation(ham), target_ev) np.testing.assert_allclose(state.expectation(ham, True), target_ev / norm) qibo.set_backend(original_backend)