def test_plot3d(): explorer = Explorer() explorer.plot3d(my_function2d, im=range(-10, 10), re=range(70)) ax = plt.gca() assert ax.xaxis.get_label().get_text() == 're' assert ax.yaxis.get_label().get_text() == 'im' maybe_show_plots()
def test_resolve_complex(): explorer = Explorer() @explorer.add_function(provides=('x'), requires=('a',)) def x(a): pass @explorer.add_function(provides=('y'), requires=('x','m','n')) def y(x): pass @explorer.add_function(provides=('m','n'), requires=('y',)) def mn(y): pass with pytest.raises(RuntimeError) as e: funcs_to_call, all_requires = explorer._resolve_call(need=('y',), have=('a',)) assert 'circular' in str(e.value) assert 'provider' in str(e.value) funcs_to_call = explorer._resolve_call(need=('y',), have=('a', 'm', 'n')) print('comlex with amn', funcs_to_call) assert funcs_to_call == (y, x) funcs_to_call = explorer._resolve_call(need=('y',), have=('x', 'm', 'n')) print('comlex with xmn', funcs_to_call) assert (funcs_to_call) == (y, )
def test_map_no_call(): explorer = Explorer(parallel='thread') cached = explorer.cache_function(my_function) data = explorer.map(cached, processes=2, x=range(5)) data_no_call = explorer.map_no_call(cached, x=range(2, 9)) print(data, data_no_call) assert data_no_call[-1] is None assert data_no_call[-2] is None assert all(data_no_call[0:3] == data[2:])
def test_caches(): explorer = Explorer() mock = MagicMock() my_function = mock.my_function wrapped = explorer.cache_function(my_function) wrapped(a=1, b=2) wrapped(a=1, b=2) my_function.assert_called_once() my_function.assert_called_once_with(a=1, b=2)
def test_mproc_cache(): explorer = Explorer(parallel='process', cache=FunctoolsCache_Mproc()) data = explorer.map(my_function, processes=2, x=range(5)) print('cache info', my_function.cache_info()) print('cache', my_function._cache) data_no_call = explorer.map_no_call(my_function, x=range(2, 9)) print(data, data_no_call) assert data_no_call[-1] is None assert data_no_call[-2] is None assert all(data_no_call[0:3] == data[2:])
def test_plot2d(): explorer = Explorer() explorer.plot2d(my_function, x=range(50)) ax = plt.gca() assert ax.xaxis.get_label().get_text() == 'x' assert len(ax.lines) == 1 line = ax.lines[0] assert all(line.get_xdata() == np.arange(0, 50)) assert all(line.get_ydata() == my_function(np.arange(0, 50))) maybe_show_plots()
def test_works_with_cache(tmpdir): """ Tests if the cache properly wraps functions to allow inspection.""" cache = JobLibCache(str(tmpdir), verbose=1) explorer = Explorer(cache=cache) @explorer.provider def c(a, b=1): return a + b z = explorer.get_variable('c', a=2) assert z == 2 + 1 z = explorer.get_variable('c', a=2, b=2) assert z == 2 + 2
def test_caches(tmpdir): """ Tests if cache indeed caches.""" cache = JobLibCache(str(tmpdir), verbose=1) explorer = Explorer(cache=cache) calls = 0 @cache def my_function(a, b): nonlocal calls calls += 1 return a + b + 1 wrapped = explorer.cache_function(my_function) print('wrapped', wrapped) wrapped(a=1, b=2) wrapped(a=1, b=2) assert calls == 1
def test_provider_and_opts(): explorer = Explorer() # Simple call @explorer.provider def sq(sum): return sum*sum @explorer.provider def sum(x, y=12): return x+y z = explorer.get_variable('sq', x=10) assert z == (10+12)**2 z = explorer.get_variable('sq', x=10, y=8) assert z == (10+8)**2
def test_maps(): explorer = Explorer() def my_function(x): return x + 1 data = explorer.map(my_function, x=range(5)) print(data) assert np.allclose(data, np.arange(1, 6)) def my_function(x, y): return x + y data = explorer.map(my_function, x=range(5), y=range(3)) print(data) assert data.shape == (5, 3) assert data[1, 2] == my_function(1, 2) assert data[4, 2] == 4 + 2
def test_add_with_provider(): explorer = Explorer() @explorer.provider def y(x, m): return x*2 + m calls_z = 0 @explorer.provider(cache=False) def z(y): nonlocal calls_z calls_z += 1 return calls_z + y zv = explorer.get_variable('z', x=1, m=5) assert zv == 1*2+1+5 zs = explorer.map_variable('z', x=[1, 2, 3], m=[2]) print(zs) assert all(zs == [6, 9, 12])
def test_resolve_linear(): explorer = Explorer() @explorer.add_function(provides=('y'), requires=('x',)) def y(x): pass @explorer.add_function(provides=('z',), requires=('y',)) def z(y): pass @explorer.add_function(provides=('k',), requires=('z',)) def k(z): pass funcs_to_call = explorer._resolve_call(need=('k',), have=('x',)) print('linear when have x', funcs_to_call) assert len(funcs_to_call) == 3 funcs_to_call = explorer._resolve_call(need=('k',), have=('y',)) print('linear when have y', funcs_to_call) assert len(funcs_to_call) == 2
def test_resolve_call(): explorer = Explorer() # Simple call @explorer.add_function(provides='version') def version(): return 12 ver = explorer.get_variable('version') assert ver == 12 # Complex call @explorer.add_function(provides=('surname','middlename'), requires='name') def surname(name): return {'John': ('Doe', None), 'Martin':('King', 'Luther')}[name] @explorer.add_function(provides='age', requires=('name',)) def age(name): return {'John': 12, 'Martin': 15}[name] @explorer.add_function(provides=('person', ), requires=('name','age','surname')) def make_person(name, surname, age): return Person(name, surname, age) person = explorer.get_variable('person', name='John') print(person) assert person.age == 12 assert person.surname == 'Doe' person = explorer.get_variable('person', name='Martin') print(person) assert person.age == 15 middle = explorer.get_variable('middlename', name='Martin') assert middle == 'Luther' people = explorer.map_variable('person', name=['Martin', 'John']) assert len(people) == 2 assert people[0].name == 'Martin' assert people[0].age == 15
def test_mproc_cache(tmpdir): cache = JobLibCache(str(tmpdir), verbose=1) # smoke explorer = Explorer(parallel='joblib', cache=cache) parallel = JobLib(n_jobs=3, verbose=10) explorer = Explorer(parallel=parallel, cache=cache) func = explorer.cache_function(my_function) start = time.time() data = explorer.map(func, processes=2, x=range(6)) dur = time.time() - start assert dur < 6 * DELAY data_no_call = explorer.map_no_call(func, x=range(3, 10)) print(data, data_no_call) assert data_no_call[-1] is None assert data_no_call[-2] is None assert all(data_no_call[0:3] == data[3:])
def test_mproc(): explorer = Explorer(parallel='process') data = explorer.map(my_function, processes=2, x=range(5)) print(data) assert np.allclose(data, np.arange(1, 6))
'ex', 'graph', 'qiskit_time', 'quimb_time', 'qtensor_time', 'gen_time_data', 'plot_xar_data', 'time_comparison_xarray', 'ql' ] # Cell import numpy as np import qtensor as qt from qtensor.tests.qiskit_qaoa_energy import simulate_qiskit_amps from qtensor.tests.qaoa_quimb import simulate_one_parallel as simulate_quimb_energy import matplotlib.pyplot as plt import time from cartesian_explorer import Explorer # Cell ex = Explorer() # Cell @ex.provider def graph(N, d=3, graph_type='random', seed=10): return qt.toolbox.random_graph(nodes=N, type=graph_type, degree=d, seed=seed) # Cell @ex.provider def qiskit_time(graph, p): gamma, beta = [.1] * p, [.3] * p
# # Compare qtensor with statevector simulations # + import sys import numpy as np import matplotlib.pyplot as plt import qtensor as qt from cartesian_explorer import Explorer # - import matplotlib as mpl from cycler import cycler mpl.rcParams['axes.prop_cycle'] = cycler(color=['#db503d', '#02C6E0']) ex = Explorer() # ## Qtensor simulation times # ### Get cost estimation # Cells in this section have metadata active="ipynb" to prevent them from running in `.py` script # + active="ipynb" N = 1000 p = 4 edge_idx = 4 gamma, beta = [.1]*p, [.3]*p graph = qt.toolbox.random_graph(nodes=N, degree=3) comp = qt.QtreeQAOAComposer(graph, gamma=gamma, beta=beta)