Commit 14d5bd87 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Adding initial pass at pyzx ir transformation



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 9d5c25b6
Pipeline #105121 passed with stage
in 71 minutes and 29 seconds
...@@ -47,5 +47,6 @@ file(GLOB PYDECORATORS benchmark/chemistry/*.py benchmark/qpt/*.py ...@@ -47,5 +47,6 @@ file(GLOB PYDECORATORS benchmark/chemistry/*.py benchmark/qpt/*.py
plugins/cirq/*.py plugins/cirq/*.py
plugins/qiskit/*.py plugins/qiskit/*.py
plugins/observables/*.py plugins/observables/*.py
plugins/pyzx/*.py
plugins/optimizers/*.py) plugins/optimizers/*.py)
install(FILES ${PYDECORATORS} DESTINATION ${CMAKE_INSTALL_PREFIX}/py-plugins) install(FILES ${PYDECORATORS} DESTINATION ${CMAKE_INSTALL_PREFIX}/py-plugins)
import xacc
qpu = xacc.getAccelerator('aer', {'sim-type':'statevector'})
q = xacc.qalloc(2)
# Create a bell state program with too many cnots
xacc.qasm('''
.compiler xasm
.circuit foo
.parameters x,y,z
.qbit q
H(q[0]);
CX(q[0], q[1]);
CX(q[0], q[1]);
CX(q[0], q[1]);
Measure(q[0]);
Measure(q[1]);
''')
f = xacc.getCompiled('foo')
# Run the python contributed IRTransformation that uses qiskit
optimizer = xacc.getIRTransformation('pyzx')
optimizer.apply(f, None, {})
# should have 4 instructions, not 6
assert(4 == f.nInstructions())
print(f.toString())
qbits = xacc.qalloc(2)
qpu.execute(qbits, f)
print(qbits)
To use this plugin, you need pyzx greater than 0.5.1. As of this
writing you have to install from source
```bash
$ git clone https://github.com/Quantomatic/pyzx && cd pyzx
$ python3 -m pip install . --user
```
\ No newline at end of file
import xacc
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
@ComponentFactory("pyzx_pass_factory")
@Provides("irtransformation")
@Property("_irtransformation", "irtransformation", "pyzx")
@Property("_name", "name", "pyzx")
@Instantiate("pyzx_pass_instance")
class PyzxIRTransformation(xacc.IRTransformation):
def __init__(self):
xacc.IRTransformation.__init__(self)
def type(self):
return xacc.IRTransformationType.Optimization
def name(self):
return 'pyzx'
def apply(self, program, accelerator, options):
# Import qiskit modules here so that users
# who don't have qiskit can still use rest of xacc
from pyzx.circuit import Circuit
from pyzx import simplify, extract, optimize
# Map CompositeInstruction program to OpenQasm string
openqasm_compiler = xacc.getCompiler('staq')
src = openqasm_compiler.translate(program).replace('\\','')
measures = []
lines = src.split('\n')
for l in lines:
if 'measure' in l or 'creg' in l:
measures.append(l)
c = Circuit.from_qasm(src)
g = c.to_graph()
simplify.full_reduce(g,quiet=True)
c2 = extract.extract_circuit(g)
c3 = optimize.basic_optimization(c2.to_basic_gates())
c3 = c3.to_basic_gates()
c3 = c3.split_phase_gates()
output = c3.to_qasm()
for measure in measures:
output += '\n' + measure
# Map the output to OpenQasm and map to XACC IR
out_prog = openqasm_compiler.compile(output, accelerator).getComposites()[0]
# update the given program CompositeInstruction reference
program.clear()
for inst in out_prog.getInstructions():
program.addInstruction(inst)
return
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment