Skip to content

Commit

Permalink
add option to choose algorithm; update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
danlkv committed Jun 3, 2020
1 parent d217dcb commit bcd95aa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
42 changes: 41 additions & 1 deletion scratchpad/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
# Usage

`python problem_graph_json.py 30 |mongocat -W -d tensim problem_graphs`
`python expr.py 30 |mongocat -W -d tensim problem_graphs`

## Search for a tensor expression

```bash
⟩ mongocat -F -d tensim expr_graphs --query '{"n_qubits":100}'
[...bson....]
```

## Optimize a tensor expression

```bash
ptyhon contr_sheme.py --ordering <ordering>
```

where ordering is one of 'qbb' or 'nghs'


## Compositions

The `contr_sheme.py` script accepts input in json, which means you can combine the
commands. Feel the power of UNIX philosophy!

### Create tensor expression and pipe it to ordering

```bash
⟩ python expr.py 47 --qaoa-layers 1 | python contr_sheme.py --ordering nghs \
| mongocat -W -d tensim contr_schemes
```


### Optimize straight from db and store the result

for example, take 100 qubits and p=1 tensor expression, and run QuickBB on it

```bash
⟩ mongocat -F -d tensim expr_graphs --query '{"n_qubits":100, "extra.p":1}' |\
python contr_sheme.py --ordering qbb \|
mongocat -W -d tensim contr_schemes
```


## Count objects in DB

Expand Down
28 changes: 23 additions & 5 deletions scratchpad/contr_sheme.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import fire
import sys
import time
import networkx as nx

import expr_optim
Expand All @@ -24,11 +25,15 @@ def read_expressions():



def as_json(read=True):
def as_json(read=True
, ordering='qbb'
):
"""
Return a JSON representation of optimized expression
"""
if ordering not in ['qbb', 'nghs']:
raise Exception("ordering should be one of 'nghs', 'qbb'")
if read:
exprs = read_expressions()
else:
Expand All @@ -41,11 +46,14 @@ def as_json(read=True):
for G, expr_id in exprs:
print(G.nodes())

peo, tw, G = expr_optim.optimize_order(G)

start_t = time.time()
peo, tw, G = expr_optim.optimize_order(G, ordering_algo=ordering)
end_t = time.time()

to_db = {}
# TODO: generator of id should be separate
to_db['_id'] = 'ordered_qbb.'+ expr_id
to_db['_id'] = f'ordered_{ordering}.'+ expr_id
to_db['expr_id'] = expr_id
# Note: mongodb will try to use all the nested dicts,
# so store the graph as string
Expand All @@ -56,9 +64,19 @@ def as_json(read=True):
except KeyError:
pass

def get_machine_id():
import socket
import getpass
host = socket.gethostname()
user = getpass.getuser()
return user + '@' + host

to_db['transforms'] =[{
'type':'order',
'params': {'order':peo, 'tw':tw, 'algo':'QuickBB'}
'type':'order'
,'contract': {'order':peo}
,'extra': {'tw':tw, 'algo': ordering
,'perf':{'time':end_t-start_t, 'run_by':get_machine_id()}
}
}]

str = json.dumps(to_db)
Expand Down
1 change: 0 additions & 1 deletion scratchpad/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def get_tensors_from_graph(graph):
return tensors

def get_id(**args):
print(args)
id = "p{p}_expr.S{S}_{type}_d{degree}_seed{seed}".format(**args)
return id

Expand Down
9 changes: 7 additions & 2 deletions scratchpad/expr_optim.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
sys.path.append('..')

import utils_qaoa
import utils
import expr
import qtree

Expand Down Expand Up @@ -89,8 +90,12 @@ def test_reorder_tensors_small():
assert perm_tensors[1]['indices'] == (0,0)
assert perm_tensors[2]['indices'] == (1,2,3)

def optimize_order(graph):
peo, tw = qtree.graph_model.get_peo(graph, int_vars=True)
def optimize_order(graph, ordering_algo='qbb'):
if ordering_algo=='qbb':
peo, tw = qtree.graph_model.get_peo(graph, int_vars=True)
if ordering_algo=='nghs':
peo, _nghs = utils.get_locale_peo(graph, utils.n_neighbors)
tw = max(_nghs) - 1
mapping = {v:i for i, v in enumerate(peo)}
tensors = expr.get_tensors_from_graph(graph)
reorder_tensors(tensors, peo)
Expand Down

0 comments on commit bcd95aa

Please sign in to comment.