Skip to content

Commit

Permalink
add draw + toquil
Browse files Browse the repository at this point in the history
  • Loading branch information
plutoniumm committed Aug 12, 2024
1 parent 385d3fe commit e59f484
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 285 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pip install abrax
```

## Examples
### toQiskit
### toPennylane
```python
from qiskit import QuantumCircuit, Parameter
from abrax import toPenny, toQasm
Expand Down Expand Up @@ -61,6 +61,7 @@ graph LR
Circuit --> B1[Pennylane]
Circuit --> C1[Cirq]
Circuit --> D1[TKet]
Circuit --> E1[Quil]
Circuit --> F1[CudaQ]
```

Expand Down
52 changes: 51 additions & 1 deletion abrax/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,54 @@ def u(self, a, b, c, u):
else:
op(*qubits)

return kernel
return kernel

def toQuil(string):
from qiskit.transpiler.passes import RemoveBarriers
from qiskit.circuit import QuantumCircuit
from pyquil import Program
import pyquil.gates as G

string, variables, params = parseVars(string)
qc = QuantumCircuit.from_qasm_str(string)
qc = RemoveBarriers()(qc)

# same as cudaq we need to manually generate the program
legal_gates = [
'I', 'X', 'Y', 'Z', 'H', 'PHASE', 'S', 'T',
'CZ', 'RX', 'RY', 'RZ', 'CX', 'CCX',
'SWAP', 'CSWAP', 'ISWAP', 'PSWAP',
]
gate_map = {
"CX": "CNOT", "CCX": "CCNOT"
}

# variables are declared as
# theta1 = p.declare("theta1", "REAL")

p = Program()
for instr, qubits, _ in qc.data:
name = instr.name.upper()
if name not in legal_gates:
raise ValueError(f"Gate {name} not supported")
arguments = instr.params
if name in gate_map:
name = gate_map[name]

op = getattr(G, name)
qubits = [q._index for q in qubits]
if len(arguments) > 0:
new_args = []
for arg in arguments:
if arg in params:
idx = params.index(arg)
arg = variables[idx]
arg = p.declare(str(arg), "REAL")
new_args.append(arg)

arguments = new_args
p += op(*new_args, *qubits)
else:
p += op(*qubits)

return p
11 changes: 7 additions & 4 deletions abrax/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cirq as cirq
import qiskit as qk

from compiler import toCirq, toQiskit, toTket, toPenny, toCudaq
from compiler import toCirq, toQiskit, toTket, toPenny, toCudaq, toQuil
from parser import toQasm

dev = qml.device("default.qubit", wires=2)
Expand Down Expand Up @@ -110,8 +110,11 @@ def bell_cudaq():
""".strip()

# print(toQiskit(QASM))
print(qml.draw(toPenny(QASM, dev))())

# print(toPenny(QASM, dev))
# print(toCirq(QASM))
# print(toTket(QASM))
# print(toCudaq(QASM))
# print(toCudaq(QASM))
# print(toQuil(QASM))
import pyquil.latex
res = pyquil.latex.to_latex(toQuil(QASM))
print(res)
25 changes: 25 additions & 0 deletions abrax/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def draw(qc, **kwargs):
name = qc.__class__.__name__
base = qc.__class__.__base__.__name__

if name == 'QuantumCircuit': # Qiskit
print(qc.draw(**kwargs))
elif name == 'QNode': # PennyLane
from pennylane import draw
print(draw(qc)(**kwargs))
elif base == 'pybind11_object': # tket
raise NotImplementedError('Drawing tket circuits is not supported yet.')
elif name == 'Circuit': # Cirq
print(qc)
elif name == 'Program': # Quil
import pyquil.latex
res = pyquil.latex.to_latex(qc, **kwargs)
print(res)

elif name == 'PyKernel': # CudaQ
from cudaq import draw
print(draw(qc, **kwargs))
else:
raise ValueError(f'Unsupported circuit: {name}')

return 0
Empty file added docs/_sidebar.md
Empty file.
139 changes: 0 additions & 139 deletions examples/cudaq.ipynb

This file was deleted.

Loading

0 comments on commit e59f484

Please sign in to comment.