def create_circuit_demo(): circuit = QCircuit(6) circuit.h_gate(0) circuit.x_gate(3) circuit.measure_all() # circuit.draw_circuit(provider=providers.IBM_PROVIDER) print(circuit.execute(provider=providers.IBM_PROVIDER, repetitions=10))
def execute(self, provider=providers.DEFAULT_PROVIDER, simulator_name=constants.DEFAULT_SIMULATOR, api=None, device=None): num_qubits = self.length circuit = QCircuit(num_qubits) self.circuit = circuit self.provider = provider for i in range(num_qubits): circuit.h_gate(i) circuit.measure_all() counts = circuit.execute(provider=provider, repetitions=1, simulator_name=simulator_name, api=api, device=device) random_number = next(iter(counts.keys())) if self.output_type == constants.DECIMAL: random_number = helper.binary_to_decimal(random_number) return random_number
def run_on_real_device(): circuit = QCircuit(1) circuit.x_gate(0) circuit.measure_all() # circuit.draw_circuit(provider=providers.GOOGLE_PROVIDER) print(circuit.execute(provider=providers.IBM_PROVIDER, repetitions=10, api=constants.IBM_API, device=constants.IBM_DEVICE_NAME))
def initialize(self, provider): self.total_qubits = ( 2 * self.num_of_qubits) + 1 if self.solution_known == 'N' else ( self.num_of_qubits + 1) self.circuit = QCircuit(self.total_qubits, self.num_of_qubits) self.provider = provider # Initialise input qubits in superposition for qubit in range(self.num_of_qubits): self.circuit.h_gate(qubit) for index in range(len(self.input_arr)): if self.input_arr[index] == 1: self.circuit.x_gate(self.num_of_qubits + index) # Initialise output qubit in the |-⟩ state. self.circuit.x_gate(self.total_qubits - 1) self.circuit.h_gate(self.total_qubits - 1)
class GroversAlgorithm: """docstring for Circuit.""" def __init__(self, clause_list=[], input_arr=[], search_keyword=None, solution_known='N', flip_output=False, num_of_iterations=None): super(GroversAlgorithm, self).__init__() self.clause_list = clause_list self.input_arr = input_arr self.search_keyword = search_keyword self.solution_known = solution_known self.flip_output = flip_output self.num_of_qubits = len( self.clause_list) if self.solution_known == 'N' else len( str(search_keyword)) self.total_qubits = 0 self.circuit = None self.provider = None if num_of_iterations is None: self.num_of_iterations = self.get_num_of_iterations() def initialize(self, provider): self.total_qubits = ( 2 * self.num_of_qubits) + 1 if self.solution_known == 'N' else ( self.num_of_qubits + 1) self.circuit = QCircuit(self.total_qubits, self.num_of_qubits) self.provider = provider # Initialise input qubits in superposition for qubit in range(self.num_of_qubits): self.circuit.h_gate(qubit) for index in range(len(self.input_arr)): if self.input_arr[index] == 1: self.circuit.x_gate(self.num_of_qubits + index) # Initialise output qubit in the |-⟩ state. self.circuit.x_gate(self.total_qubits - 1) self.circuit.h_gate(self.total_qubits - 1) def oracle_for_unknown_solution(self): output_qubit = self.total_qubits - 1 clause_qubits = [] for index in range(len(self.clause_list)): clause_qubit = self.num_of_qubits + index self.XOR(self.clause_list[index], clause_qubit) clause_qubits.append(clause_qubit) if self.flip_output: for index in range(len(clause_qubits)): self.circuit.x_gate(clause_qubits[index]) # Flip 'output' bit if all clauses are satisfied self.circuit.mct_gate(clause_qubits, output_qubit) if self.flip_output: for index in range(len(clause_qubits)): self.circuit.x_gate(clause_qubits[index]) for index in range(len(self.clause_list)): clause_qubit = self.num_of_qubits + index self.XOR(self.clause_list[index], clause_qubit) def oracle_for_known_solution(self): output_qubit = self.total_qubits - 1 input_qubits = [] keyword = self.search_keyword for index in range(len(str(keyword))): if not int(str(keyword)[index]): self.circuit.x_gate(index) input_qubits.append(index) self.circuit.mct_gate(input_qubits, output_qubit) for index in range(len(str(keyword))): if not int(str(keyword)[index]): self.circuit.x_gate(index) def XOR(self, qubits, output_qubit): for qubit in qubits: self.circuit.cx_gate(qubit, output_qubit) def diffuser(self): q_circuit = self.circuit qubits = self.num_of_qubits for qubit in range(qubits): q_circuit.h_gate(qubit) for qubit in range(qubits): q_circuit.x_gate(qubit) q_circuit.h_gate(qubits - 1) q_circuit.mct_gate(list(range(qubits - 1)), qubits - 1) q_circuit.h_gate(qubits - 1) for qubit in range(qubits): q_circuit.x_gate(qubit) for qubit in range(qubits): q_circuit.h_gate(qubit) def execute(self, provider=providers.DEFAULT_PROVIDER, repetitions=10): self.initialize(provider) for _ in range(self.num_of_iterations): if self.solution_known == 'N': self.oracle_for_unknown_solution() else: self.oracle_for_known_solution() self.diffuser() for i in range(self.num_of_qubits): self.circuit.measure(i) return self.circuit.execute(provider=provider, repetitions=repetitions) def draw_grovers_circuit(self): return self.circuit.draw_circuit(provider=self.provider) def get_num_of_iterations(self): return int( 0.5 * (0.5 * np.pi / np.arcsin(1 / (np.sqrt(2**self.num_of_qubits))) - 1))
def __init__(self, sequence): super(ProteinFolding, self).__init__() self.sequence = sequence self.length = 2 # two's complements: # 0 = 000, 1 = 001, 2 = 010, 3 = 011, -1 = 111, -2 = 110, -3 = 101 # (-4 = 100, not needed) # For the purpose of this simplified validation we don't need the fixed # coordinates of the first amino acid which are all 0 # quantum register holding the x coordinates for x1, x2 etc. (x0 = 000 omitted) self.x = [0, 1, 2] # quantum register holding the y coordinates for y1, y2 etc. (y0 = 000 omitted) self.y = [3, 4, 5] # quantum register holding the y coordinates for z1, z2 etc. (z0 = 000 omitted) self.z = [6, 7, 8] # quantum register holding the controls w0, w1, etc. self.w = [9, 10, 11] # register holding the binary 1 (3 qubits) self.a = [12, 13, 14] # register holding the two's complement of 1 (3 qubits) not needed, # can be replaced by a with the first 2 qubits negated. # register holding the carry bit for ripple-carry adder self.c = [15] # quantum register that holds the energy value for each conformation: if first and # last amino acid are located diagonally # from each other, e = 1, otherwise e = 0. # There are 4 conformations for which e = 1. self.e = [16] # ancilla qubits, at most three needed for the ccrca function self.anc = [17, 18, 19] self.circuit = QCircuit(20) # encoding binary 1 self.circuit.x_gate(self.a[2]) # encoding the two's complement of 1 #qc.x(t[0:length]) # setting the state into superposition #qc.h_gate(w[0:3]) for i in range(len(self.w)): self.circuit.h_gate(self.w[i]) # setting energy qubit e into superposition on par with |w> self.circuit.h_gate(self.e[0]) # global variable used in Subroutine 1 to navigate among the values of vector w self.b = 0 self.arglist = [] # Subroutine 1: Generating conformational space for d in range(2, self.length + 1): for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) # if w[0]=1 then x+1, if w[0]=0 then x-1 self.arglist.append(self.w[self.b]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) #range [0,1,2] for d=2, range [3,4,5] for d=3 for i in range(0, 3): self.arglist.append(self.x[i]) for i in range(0, 3): self.arglist.append(self.a[i]) #print(arglist) CCRCA(self.circuit, self.arglist) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.circuit.x_gate(self.w[self.b]) self.arglist.append(self.w[self.b]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) for i in range(0, 3): self.arglist.append(self.x[i]) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA(self.circuit, self.arglist) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) self.circuit.x_gate(self.w[self.b]) # if w[1]=1 then y+1, if w[1]=0 then y-1 for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.arglist.append(self.w[self.b + 1]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) #range [0,1,2] for d=2, range [3,4,5] for d=3 for i in range(0, 3): self.arglist.append(self.y[i]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA(self.circuit, self.arglist) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.circuit.x_gate(self.w[self.b + 1]) self.arglist.append(self.w[self.b + 1]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) for i in range(0, 3): self.arglist.append(self.y[i]) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA(self.circuit, self.arglist) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) self.circuit.x_gate(self.w[self.b + 1]) # if w[2]=1 then z+1, if w[2]=0 then z-1 for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.arglist.append(self.w[self.b + 2]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) #range [0,1,2] for d=2, range [3,4,5] for d=3 for i in range(0, 3): self.arglist.append(self.z[i]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA(self.circuit, self.arglist) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.circuit.x_gate(self.w[self.b + 2]) self.arglist.append(self.w[self.b + 2]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) for i in range(0, 3): self.arglist.append(self.z[i]) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA(self.circuit, self.arglist) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) self.circuit.x_gate(self.w[self.b + 2]) #b = b+3 # Subroutine 2: Finding an arbitrary conformation, e.g. |w>=|110> which is # a transition downwards out of the page. # There are a total of 8 conformations for a sequence of length L=2. # For this conformation, the energy value will be |e>=|1>, otherwise it will be |0>. self.circuit.h_gate(self.e[0]) self.circuit.x_gate(self.w[2]) self.circuit.ccx_gate(self.w[0], self.w[1], self.anc[0]) self.circuit.ccx_gate(self.anc[0], self.w[2], self.e[0]) # uncomputing self.circuit.ccx_gate(self.w[0], self.w[1], self.anc[0]) self.circuit.x_gate(self.w[2]) self.circuit.h_gate(self.e[0]) # Subroutine 3: Uncomputation of coordinates by running Subroutine 1 in reverse self.b = 0 for d in range(self.length, 1, -1): for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) # if w[0]=1 then x+1, if w[0]=0 then x-1 self.arglist.append(self.w[self.b]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) #range [0,1,2] for d=2, range [3,4,5] for d=3 for i in range(0, 3): self.arglist.append(self.x[i]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA_INVERSE(self.circuit, self.arglist) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.circuit.x_gate(self.w[self.b]) self.arglist.append(self.w[self.b]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) for i in range(0, 3): self.arglist.append(self.x[i]) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA_INVERSE(self.circuit, self.arglist) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) self.circuit.x_gate(self.w[self.b]) # if w[1]=1 then y+a, if w[1]=0 then y-a for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.arglist.append(self.w[self.b + 1]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) #range [0,1,2] for d=2, range [3,4,5] for d=3 for i in range(0, 3): self.arglist.append(self.y[i]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA_INVERSE(self.circuit, self.arglist) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.circuit.x_gate(self.w[self.b + 1]) self.arglist.append(self.w[self.b + 1]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) for i in range(0, 3): self.arglist.append(self.y[i]) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA_INVERSE(self.circuit, self.arglist) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) self.circuit.x_gate(self.w[self.b + 1]) # if w[2]=1 then z+1, if w[2]=0 then z-1 for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.arglist.append(self.w[self.b + 2]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) #range [0,1,2] for d=2, range [3,4,5] for d=3 for i in range(0, 3): self.arglist.append(self.z[i]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA_INVERSE(self.circuit, self.arglist) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) self.circuit.x_gate(self.w[self.b + 2]) self.arglist.append(self.w[self.b + 2]) for i in range(0, 3): self.arglist.append(self.anc[i]) self.arglist.append(self.c[0]) for i in range(0, 3): self.arglist.append(self.z[i]) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) for i in range(0, 3): self.arglist.append(self.a[i]) CCRCA_INVERSE(self.circuit, self.arglist) self.circuit.x_gate(self.a[0]) self.circuit.x_gate(self.a[1]) self.circuit.x_gate(self.w[self.b + 2]) for i in range(len(self.arglist) - 1, -1, -1): self.arglist.pop(i) #b = b-3 return
def make_circuit(self, num_qubits, sop): qc = QCircuit(num_qubits*3) #self.qc = qc for i in range(num_qubits): qc.h_gate(i) #qc.barrier() for i in range(num_qubits): qc.cx_gate(i, i+(num_qubits*2)) qc.x_gate(i+(num_qubits*2)) cnt=0 #qc.barrier() for o in sop: for i in range(len(o)): lst=[] if(o[i]!=0): for j in range(num_qubits): if(self.nth_bit(o[i]-1,j)==1): lst.append(j) else: lst.append(j+(num_qubits*2)) qc.mct_gate(lst,cnt+num_qubits) cnt+=1 #qc.barrier() for i in range(num_qubits): qc.measure(num_qubits+i) self.qc=qc return qc