Drop previous parser
This commit is contained in:
@@ -5,127 +5,6 @@ import quimb.tensor as qtn
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def get_gate_params(operation):
|
|
||||||
if "h " in operation:
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no.insert(0, "H")
|
|
||||||
elif "x " in operation:
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no.insert(0, "X")
|
|
||||||
elif "y " in operation:
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no.insert(0, "Y")
|
|
||||||
elif "z " in operation:
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no.insert(0, "Z")
|
|
||||||
elif "s " in operation:
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no.insert(0, "S")
|
|
||||||
elif "t " in operation:
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no.insert(0, "T")
|
|
||||||
elif "cu1" in operation:
|
|
||||||
lambda_ = float(
|
|
||||||
".".join(re.findall(
|
|
||||||
r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]))
|
|
||||||
)
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no[0:0] = ["CU1", lambda_]
|
|
||||||
elif "cu2" in operation:
|
|
||||||
angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])
|
|
||||||
phi = float(".".join(angles[0:2]))
|
|
||||||
lambda_ = float(".".join(angles[2:]))
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no[0:0] = ["CU2", phi, lambda_]
|
|
||||||
elif "cu3" in operation:
|
|
||||||
angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])
|
|
||||||
theta = float(".".join(angles[0:2]))
|
|
||||||
phi = float(".".join(angles[2:4]))
|
|
||||||
lambda_ = float(".".join(angles[4:]))
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no[0:0] = ["CU3", theta, phi, lambda_]
|
|
||||||
elif " cx " in operation:
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no.insert(0, "CX")
|
|
||||||
elif " cy " in operation:
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no.insert(0, "CY")
|
|
||||||
elif " cz " in operation:
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no.insert(0, "CZ")
|
|
||||||
elif " ccx " in operation:
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no.insert(0, "CCX")
|
|
||||||
elif " ccy " in operation:
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no.insert(0, "CCY")
|
|
||||||
elif " ccz " in operation:
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no.insert(0, "CCZ")
|
|
||||||
elif " rx " in operation:
|
|
||||||
theta = float(
|
|
||||||
".".join(re.findall(
|
|
||||||
r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]))
|
|
||||||
)
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no[0:0] = ["RX", theta]
|
|
||||||
elif "^ry " in operation:
|
|
||||||
theta = float(
|
|
||||||
".".join(re.findall(
|
|
||||||
r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]))
|
|
||||||
)
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no[0:0] = ["RY", theta]
|
|
||||||
elif "^rz " in operation:
|
|
||||||
theta = float(
|
|
||||||
".".join(re.findall(
|
|
||||||
r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]))
|
|
||||||
)
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no[0:0] = ["RZ", theta]
|
|
||||||
elif "^rzz " in operation:
|
|
||||||
theta = float(
|
|
||||||
".".join(re.findall(
|
|
||||||
r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]))
|
|
||||||
)
|
|
||||||
qbit_no = re.findall(r"\d+", operation.split(" ")[1])
|
|
||||||
qbit_no = [int(x) for x in qbit_no]
|
|
||||||
qbit_no[0:0] = ["RZZ", theta]
|
|
||||||
elif "^u1 " in operation:
|
|
||||||
lambda_ = float(
|
|
||||||
".".join(re.findall(
|
|
||||||
r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0]))
|
|
||||||
)
|
|
||||||
qbit_no = [int(re.findall(r"\d+", operation)[0])]
|
|
||||||
qbit_no[0:0] = ["U1", lambda_]
|
|
||||||
elif "^u2 " in operation:
|
|
||||||
angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])
|
|
||||||
phi = float(".".join(angles[0:2]))
|
|
||||||
lambda_ = float(".".join(angles[2:]))
|
|
||||||
qbit_no = int(re.findall(r"\d+", operation)[0])
|
|
||||||
qbit_no[0:0] = ["U2", phi, lambda_] # pylint: disable=E1137
|
|
||||||
elif "^u3 " in operation:
|
|
||||||
angles = re.findall(r"\b\d+(?:[Ee][+-]?\d+)?", operation.split(" ")[0])
|
|
||||||
theta = float(".".join(angles[0:2]))
|
|
||||||
phi = float(".".join(angles[2:4]))
|
|
||||||
lambda_ = float(".".join(angles[4:]))
|
|
||||||
qbit_no = int(re.findall(r"\d+", operation)[0])
|
|
||||||
qbit_no[0:0] = ["U3", theta, phi, lambda_] # pylint: disable=E1137
|
|
||||||
else:
|
|
||||||
assert "Unsupported gate"
|
|
||||||
|
|
||||||
return qbit_no
|
|
||||||
|
|
||||||
|
|
||||||
def get_gate_functions(qasm_str, start_idx):
|
def get_gate_functions(qasm_str, start_idx):
|
||||||
func_list = []
|
func_list = []
|
||||||
result = []
|
result = []
|
||||||
@@ -179,7 +58,8 @@ def init_state_tn(nqubits, init_state_sv, tn_lib="quimb"):
|
|||||||
|
|
||||||
if tn_lib == "quimb":
|
if tn_lib == "quimb":
|
||||||
init_state_MPS = qtn.tensor_1d.MatrixProductState.from_dense(
|
init_state_MPS = qtn.tensor_1d.MatrixProductState.from_dense(
|
||||||
init_state_sv, dims)
|
init_state_sv, dims
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# TODO: Add cuquantum later
|
# TODO: Add cuquantum later
|
||||||
assert False, "Unsupported tensor network backend in initilization"
|
assert False, "Unsupported tensor network backend in initilization"
|
||||||
@@ -187,8 +67,9 @@ def init_state_tn(nqubits, init_state_sv, tn_lib="quimb"):
|
|||||||
return init_state_MPS
|
return init_state_MPS
|
||||||
|
|
||||||
|
|
||||||
def tn_circ_eval(nqubits, qasm_circ, init_state, swaps=True, tn_lib="quimb",
|
def tn_circ_eval(
|
||||||
backend='numpy'):
|
nqubits, qasm_circ, init_state, swaps=True, tn_lib="quimb", backend="numpy"
|
||||||
|
):
|
||||||
if tn_lib == "quimb":
|
if tn_lib == "quimb":
|
||||||
|
|
||||||
circ_quimb = qasm_QFT(nqubits, qasm_circ, swaps, psi0=init_state)
|
circ_quimb = qasm_QFT(nqubits, qasm_circ, swaps, psi0=init_state)
|
||||||
@@ -205,7 +86,11 @@ def eval_QI_qft(nqubits, qasm_circ, init_state, backend="numpy", swaps=True):
|
|||||||
|
|
||||||
# Quimb circuit
|
# Quimb circuit
|
||||||
init_state_mps = init_state_tn(nqubits=nqubits, init_state_sv=init_state)
|
init_state_mps = init_state_tn(nqubits=nqubits, init_state_sv=init_state)
|
||||||
amplitudes = tn_circ_eval(nqubits=nqubits, qasm_circ=qasm_circ,
|
amplitudes = tn_circ_eval(
|
||||||
init_state=init_state_mps, swaps=swaps,
|
nqubits=nqubits,
|
||||||
tn_lib="quimb")
|
qasm_circ=qasm_circ,
|
||||||
|
init_state=init_state_mps,
|
||||||
|
swaps=swaps,
|
||||||
|
tn_lib="quimb",
|
||||||
|
)
|
||||||
return amplitudes
|
return amplitudes
|
||||||
|
|||||||
@@ -11,8 +11,7 @@ import config
|
|||||||
|
|
||||||
|
|
||||||
def init_state_sv(nqubits):
|
def init_state_sv(nqubits):
|
||||||
init_state = np.random.random(2**nqubits) + \
|
init_state = np.random.random(2**nqubits) + 1j * np.random.random(2**nqubits)
|
||||||
1j * np.random.random(2**nqubits)
|
|
||||||
init_state = init_state / np.sqrt((np.abs(init_state) ** 2).sum())
|
init_state = init_state / np.sqrt((np.abs(init_state) ** 2).sum())
|
||||||
# An unmodified init_state has to be converted to tn format
|
# An unmodified init_state has to be converted to tn format
|
||||||
init_state_for_tn = copy.deepcopy(init_state)
|
init_state_for_tn = copy.deepcopy(init_state)
|
||||||
@@ -33,25 +32,28 @@ def test_eval(nqubits: int):
|
|||||||
init_state_qibo, init_state_for_tn = init_state_sv(nqubits=nqubits)
|
init_state_qibo, init_state_for_tn = init_state_sv(nqubits=nqubits)
|
||||||
|
|
||||||
# Test qibo
|
# Test qibo
|
||||||
qibo.set_backend(backend=config.qibo['backend'],
|
qibo.set_backend(backend=config.qibo["backend"], platform=config.qibo["platform"])
|
||||||
platform=config.qibo['platform'])
|
|
||||||
start_time = timer()
|
start_time = timer()
|
||||||
qibo_circ, result_sv = qibo_qft(nqubits, init_state=init_state_qibo,
|
qibo_circ, result_sv = qibo_qft(
|
||||||
swaps=config.qibo['swaps'])
|
nqubits, init_state=init_state_qibo, swaps=config.qibo["swaps"]
|
||||||
|
)
|
||||||
end_time = timer()
|
end_time = timer()
|
||||||
qibo_time = end_time - start_time
|
qibo_time = end_time - start_time
|
||||||
|
|
||||||
# Convert to qasm for other backends
|
# Convert to qasm for other backends
|
||||||
qasm_circ = qibo_circ.to_qasm()
|
qasm_circ = qibo_circ.to_qasm()
|
||||||
|
__import__("pdb").set_trace()
|
||||||
|
|
||||||
# Test quimb
|
# Test quimb
|
||||||
start_time = timer()
|
start_time = timer()
|
||||||
result_tn = qasm_quimb.eval_QI_qft(nqubits=nqubits, qasm_circ=qasm_circ,
|
result_tn = qasm_quimb.eval_QI_qft(
|
||||||
init_state=init_state_for_tn,
|
nqubits=nqubits,
|
||||||
backend=config.quimb['backend'],
|
qasm_circ=qasm_circ,
|
||||||
swaps=config.quimb['swaps'])
|
init_state=init_state_for_tn,
|
||||||
|
backend=config.quimb["backend"],
|
||||||
|
swaps=config.quimb["swaps"],
|
||||||
|
)
|
||||||
end_time = timer()
|
end_time = timer()
|
||||||
quimb_time = end_time - start_time
|
quimb_time = end_time - start_time
|
||||||
|
|
||||||
assert np.allclose(result_sv, result_tn), \
|
assert np.allclose(result_sv, result_tn), "Resulting dense vectors do not match"
|
||||||
"Resulting dense vectors do not match"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user