From 6137d2c50958875bc4ff6fe41211422e1e0c9fb1 Mon Sep 17 00:00:00 2001 From: Danil Date: Fri, 26 Apr 2024 07:02:24 +0000 Subject: [PATCH] add test for compressed energy exp calculation --- qtensor/compression/compressed_contraction.py | 2 +- .../test_compressed_energy_expectation.py | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 qtensor/compression/tests/test_compressed_energy_expectation.py diff --git a/qtensor/compression/compressed_contraction.py b/qtensor/compression/compressed_contraction.py index 893987f9..1023021c 100644 --- a/qtensor/compression/compressed_contraction.py +++ b/qtensor/compression/compressed_contraction.py @@ -203,5 +203,5 @@ def compressed_sum(A:Tensor, sum_ixs, R = Tensor(new_tensor_name, result_indices, data=chunk) else: R.set_chunk(r_i, chunk) - compressor.compressor.free_decompressed() + compressor.free_decompressed() return R diff --git a/qtensor/compression/tests/test_compressed_energy_expectation.py b/qtensor/compression/tests/test_compressed_energy_expectation.py new file mode 100644 index 00000000..895999f9 --- /dev/null +++ b/qtensor/compression/tests/test_compressed_energy_expectation.py @@ -0,0 +1,24 @@ +import qtensor +import numpy as np +from qtensor.compression import CUSZPCompressor +import qtensor.tests + +def test_compress_energy_expect(): + G, gamma, beta = qtensor.tests.get_test_problem(n=10, p=2, type='random') + edge = list(G.edges())[0] + composer = qtensor.QtreeQAOAComposer(G, gamma=gamma, beta=beta) + composer.energy_expectation_lightcone(edge) + circuit = composer.circuit + base_backend = qtensor.contraction_backends.get_backend('cupy') + compressor = CUSZPCompressor(r2r_error=1e-4, r2r_threshold=1e-4) + backend = qtensor.contraction_backends.CompressionBackend(base_backend, compressor, max_tw=6) + sim = qtensor.QtreeSimulator(backend=backend) + res = sim.simulate(circuit) + sim_exact = qtensor.QtreeSimulator(backend=base_backend) + ref = sim_exact.simulate(circuit) + print(f'exact: {ref}, compressed: {res}') + assert np.allclose(res, ref, atol=1e-4, rtol=0.05) + +if __name__ == '__main__': + test_energy_expect() + print('test passed!')