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 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
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)
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) # print("->") # print(resulting_state) state = list_to_circuit( [H(i) for i in qbits_px]) * State.new_zero_state(len(ancillas) + len(qbits_px)) print(state) print("->") print(circuit * state)
def CRX(act, control, phi): return (CX(act, control) | (H(act) | R(act, -phi) | X(act) | R(act, phi) | X(act) | H(act)) | CX(act, control))
def C2X(act, c1, c2): return (H(act) | CX(act, c2) | T(act).get_dagger() | CX(act, c1) | T(act) | CX(act, c2) | T(act).get_dagger() | CX(act, c1) | T(act) | H(act) | T(c2).get_dagger() | CX(c2, c1) | T(c2).get_dagger() | CX(c2, c1) | T(c1) | S(c2))
def CRY(act, control, phi): return (CX(act, control) | (S(act) | H(act) | R(act, -phi) | X(act) | R(act, phi) | X(act) | H(act) | S(act).get_dagger()) | CX(act, control))