def Ttrot_potential_optimized_8qbits(qbits_px, ancillas, dt, V0): return (QFT(qbits_px).get_dagger() | list_to_circuit([X(i) for i in qbits_px[-5:]]) | ncontrolled(qbits_px[0], CRI, ancillas, qbits_px[-5:], -dt * V0 / 2) | list_to_circuit([X(i) for i in qbits_px[-5:]]) | QFT(qbits_px))
def Ttrot_potential(qbits_px, ancillas, dt, V0): return (QFT(qbits_px).get_dagger() | list_to_circuit([X(i) for i in qbits_px[-5:]]) | C5R(qbits_px[0], qbits_px[-5:], ancillas, -dt * V0 / 2) | C5X(qbits_px[0], qbits_px[-5:], ancillas) | C5R(qbits_px[0], qbits_px[-5:], ancillas, -dt * V0 / 2) | C5X(qbits_px[0], qbits_px[-5:], ancillas) | list_to_circuit([X(i) for i in qbits_px[-5:]]) | QFT(qbits_px))
def QuantumEvolution(data, n_sample, iSt=pyqcs.State.new_zero_state(1), n_measure=1000, log=False): probs = data[:, 0] circs = np.ndarray((probs.shape[0], 2), dtype=pyqcs.AnonymousCompoundGateCircuit) for i, d in enumerate(data): c1 = qu.su2_to_circuit(0, d[1:4]) c2 = qu.su2_to_circuit(0, d[4:]) circs[i, :] = [c1, c2] output = np.zeros((n_sample, 2), dtype=float) paths = [] for i in range(n_sample): evolute_gates = np.ndarray(probs.shape[0], dtype=pyqcs.AnonymousCompoundGateCircuit) path = "" for k, p in enumerate(probs): q = np.random.rand() if q < p: evolute_gates[k] = circs[k, 0] path += "0" else: evolute_gates[k] = circs[k, 1] path += "1" U_gate = pyqcs.list_to_circuit(evolute_gates) psi = U_gate * iSt res = pyqcs.sample(psi, 1, n_measure) paths.append(path) for key, val in res.items(): output[i, key] = float(val) / n_measure paths = np.array(paths) means = np.zeros(10, dtype=np.double) for i in range(10): tmp = np.random.choice(output[:, 0], size=data.shape[0], replace=True) means[i] = np.mean(tmp) mean0, err0 = st.bootstrap(output[:, 0]) mean1, err1 = st.bootstrap(output[:, 1]) if log: return mean0, err0, mean1, err1, paths else: return mean0, err0, mean1, err1
def QFT(bits): if (isinstance(bits, int)): bits = [i for i in range(bits.bit_length() + 1) if bits & (1 << i)] if (not isinstance(bits, (list, tuple))): raise TypeError("bits must be either int, list or tuple") reversed_bits = list(reversed(bits)) circuit = [ H(qbit) | cascading_crk(i, reversed_bits) for i, qbit in enumerate(reversed_bits) ] circuit = list_to_circuit(circuit) nbits = len(bits) swap_bits = list_to_circuit( [SWAP(bits[i], bits[nbits - i - 1]) for i in range(nbits // 2)]) return circuit | swap_bits
def Ttrot(qbits_phi, qbits_px, qbits_py, dt, c, momentum_omegas): circuit_list = [] for qb_phi in qbits_phi: for j, qb_px in enumerate(qbits_px): circuit_list.append( CRX(qb_phi, qb_px, -dt * c**2 * momentum_omegas[j])) for j, qb_py in enumerate(qbits_py): circuit_list.append( CRY(qb_phi, qb_py, -dt * c**2 * momentum_omegas[j])) return list_to_circuit(circuit_list)
def u2_to_circuit(act, param): phi = param[0] gates = [] gates.append(phase(act, phi / 2)) su2gates = su2_to_circuit(act, param[1:]) gates.append(su2gates) return pyqcs.list_to_circuit(gates)
def su2_to_circuit(act, param): phi0, phi1, phi2 = param th0 = phi0 th1 = 1 / 2 * (phi1 + phi2) th2 = 1 / 2 * (phi1 - phi2) S = R(act, np.pi / 2) Sd = R(act, -np.pi / 2) gates = [] gates.append(expIphiZ(act, th2)) gates.append(Sd | H(act) | expIphiZ(act, th0) | H(act) | S) gates.append(expIphiZ(act, th1)) return pyqcs.list_to_circuit(gates)
def prepare_momentum_eigenstate(nqbits, qbits_px, qbits_py, qbits_phi, px_init, py_init, phi_init, antifermion): with log_time(__name__, "prepare_momentum_eigenstate"): state = State.new_zero_state(nqbits) circuit_px_init = [ X(q) for i, q in enumerate(qbits_px) if px_init & (1 << i) ] circuit_px_init = list_to_circuit(circuit_px_init) circuit_py_init = [ X(q) for i, q in enumerate(qbits_py) if py_init & (1 << i) ] circuit_py_init = list_to_circuit(circuit_py_init) if (not antifermion): state = (H(qbits_phi[0]) | RZ(qbits_phi[0], phi_init) | circuit_px_init | circuit_py_init) * state else: state = (X(qbits_phi[0]) | H(qbits_phi[0]) | RZ(qbits_phi[0], phi_init) | circuit_px_init | circuit_py_init) * state return state
def TtrotLandau(qbits_phi, qbits_px, qbits_py, dt, c, B, momentum_omegas): circuit_list = [] for qb_phi in qbits_phi: circuit_list.append(QFT(qbits_px).get_dagger()) for j, qb_px in enumerate(qbits_px): circuit_list.append( CRY(qb_phi, qb_px, -dt * c * B * momentum_omegas[j])) circuit_list.append(QFT(qbits_px).get_dagger()) circuit_list.append(QFT(qbits_py)) for j, qb_py in enumerate(qbits_py): circuit_list.append( CRX(qb_phi, qb_py, dt * c * B * momentum_omegas[j])) circuit_list.append(QFT(qbits_py)) return list_to_circuit(circuit_list)
def ncontrolled(act, cgate, ancillas, controls, *args): """ Takes an 1-controlled gate ``cgate`` and builds an ``n``-controlled gate, where ``n = len(controls)`` using the ``n - 1`` ``ancillas``. ``*args`` will be passed to ``cgate`` after the act and control arguments. """ if (len(ancillas) < len(controls) - 1): raise ValueError( f"need len(controls) - 1 ancillas (expected {len(controls) -1}, got {len(ancillas)})" ) if (len(ancillas) > len(controls) - 1): warnings.warn( f"excessive ancilla qbits used, this may lead to bad performance (expected {len(controls) -1}, got {len(ancillas)})", ResourceWarning) ancillas = ancillas[:len(controls) - 1] handthrough_circuit = [C2X(ancillas[0], controls[0], controls[1])] for i, cancilla in enumerate(ancillas[:-1]): handthrough_circuit.append( C2X(ancillas[i + 1], controls[i + 2], cancilla)) handthrough_circuit = list_to_circuit(handthrough_circuit) return handthrough_circuit | cgate( act, ancillas[-1], *args) | handthrough_circuit.get_dagger()
import numpy as np from pyqcs import H, list_to_circuit, PrettyState as State from lib.circuits import CRI state = list_to_circuit([H(i) for i in range(2)]) * State.new_zero_state(2) print(state) print("->") print(CRI(0, 1, np.pi / 2) * state)
from itertools import product import numpy as np from pyqcs import X, H, list_to_circuit, PrettyState as State from lib.circuits import CR from lib.controlled_gate import ncontrolled input_states = list(product(*([[0, 1]] * 8))) qbits_px = list(range(8)) ancillas = list(range(8, 8 + 5)) print(input_states) circuit = (list_to_circuit([X(i) for i in qbits_px[-6:]]) | ncontrolled(qbits_px[0], CR, ancillas, qbits_px[-6:], np.pi / 2) | list_to_circuit([X(i) for i in qbits_px[-6:]])) circuit = ncontrolled(qbits_px[0], CR, ancillas, qbits_px[-6:], np.pi / 2) print(qbits_px) print(qbits_px[-6:]) #for config in input_states: # print("-"*80) # setup_circuit = list_to_circuit([X(i) for i,j in enumerate(config) if j]) # state = State.new_zero_state(len(ancillas) + len(qbits_px)) # state = setup_circuit * state # # resulting_state = circuit * state # # print(state)
from itertools import product from pyqcs import State, X, list_to_circuit from lib.circuits import C2X configurations = list(product([0, 1], [0, 1], [0, 1])) for config in configurations: print("#" * 60) print(config) setup = list_to_circuit([X(i) for i, s in enumerate(config) if s]) state = setup * State.new_zero_state(3) print(state) print(C2X(2, 1, 0) * state)
def cascading_crk(i, bits): circuit_list = [ CRk(bits[i], c, j + 2) for j, c in reversed(list(enumerate(bits[i + 1:]))) ] return list_to_circuit(circuit_list)
def control5_handthrough(controls, ancillas): circuit = [C2X(ancillas[0], controls[0], controls[1])] for i, cancilla in enumerate(ancillas[:-1]): circuit.append(C2X(ancillas[i + 1], controls[i + 2], cancilla)) return list_to_circuit(circuit)
def QuantumEvolution(data, n_sample, iSt=pyqcs.State.new_zero_state(1), n_measure=1000, log=False, filename=""): if log == True and filename == "": path = "./QuEvoData" if not os.path.exists(path): os.makedirs(path) filename = path + "/data.txt" probs = data[:, 0] circs = np.ndarray((probs.shape[0], 2), dtype=pyqcs.AnonymousCompoundGateCircuit) for i, d in enumerate(data): c1 = qu.su2_to_circuit(0, d[1:4]) c2 = qu.su2_to_circuit(0, d[4:]) circs[i, :] = [c1, c2] output = np.zeros((n_sample, 2), dtype=float) paths = [] for i in range(n_sample): evolute_gates = np.ndarray(probs.shape[0], dtype=pyqcs.AnonymousCompoundGateCircuit) path = "" for k, p in enumerate(probs): q = np.random.rand() if q < p: evolute_gates[k] = circs[k, 0] path += "0" else: evolute_gates[k] = circs[k, 1] path += "1" U_gate = pyqcs.list_to_circuit(evolute_gates) psi = U_gate * iSt res = pyqcs.sample(psi, 1, n_measure) paths.append(path) for key, val in res.items(): output[i, key] = float(val) / n_measure paths = np.array(paths) if log: file = open(filename, "w") for o in output: file.write("{}\t{}\n".format(o[0], o[1])) file.close() means = np.zeros(10, dtype=np.double) for i in range(10): tmp = np.random.choice(output[:, 0], size=data.shape[0], replace=True) means[i] = np.mean(tmp) mean0 = np.mean(output[:, 0]) mean1 = np.mean(output[:, 1]) err0 = np.std(output[:, 0]) err1 = np.std(output[:, 1]) #mean0, err0 = st.bootstrap(output[:,0]) #mean1, err1 = st.bootstrap(output[:,1]) return mean0, err0, mean1, err1