Skip to content

Commit

Permalink
Handle larger circuits for QrackCircuit conversion to Qiskit circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Nov 15, 2024
1 parent a7bc72b commit 1d2e1d6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
45 changes: 37 additions & 8 deletions pyqrack/qrack_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
try:
from qiskit.circuit.quantumcircuit import QuantumCircuit
from qiskit.compiler.transpiler import transpile
from qiskit.circuit.library import UCGate
from qiskit.circuit.library import U3Gate, UCGate
import numpy as np
import math
except ImportError:
Expand All @@ -31,6 +31,26 @@
except ImportError:
_IS_TENSORCIRCUIT_AVAILABLE = False


def euler_angles_1q(m):
if len(m) != 2:
raise ValueError("euler_angles_1q: expected flat 2x2 matrix")

phase = (m[0] * m[3] - m[1] * m[2]) ** (-1.0/2.0)
U = phase * m

theta = 2 * math.atan2(abs(U[1]), abs(U[0]))

# Find phi and lambda
phiplambda = 2 * np.angle(U[3])
phimlambda = 2 * np.angle(U[1])

phi = (phiplambda + phimlambda) / 2.0
lamb = (phiplambda - phimlambda) / 2.0

return theta, phi, lamb


class QrackCircuit:
"""Class that exposes the QCircuit class of Qrack
Expand Down Expand Up @@ -286,13 +306,22 @@ def file_to_qiskit_circuit(filename):
payloads[key] = np.array(op)

gate_list = []
for j in range(1 << control_count):
if j in payloads:
gate_list.append(payloads[j])
else:
gate_list.append(np.array([[1, 0],[0, 1]]))

circ.append(UCGate(gate_list), controls + [target])
control_pow = 1 << control_count
pLen = len(payloads)
if (pLen == 1) or ((pLen < (control_pow >> 1)) and (control_pow > 20)):
for c, p in payloads.items():
theta, phi, lam = euler_angles_1q(p)
circ.append(
U3Gate(theta, phi, lam).control(control_count, c),
controls + [target]
)
else:
for j in range(control_pow):
if j in payloads:
gate_list.append(payloads[j])
else:
gate_list.append(np.array([[1, 0],[0, 1]]))
circ.append(UCGate(gate_list), controls + [target])

return circ

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup


VERSION = "1.32.19"
VERSION = "1.32.20"

# Read long description from README.
README_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'README.md')
Expand Down

0 comments on commit 1d2e1d6

Please sign in to comment.