def logical_or(self, qr_variables, qb_target, qr_ancillae, flags=None, mct_mode='basic'): """Build a collective disjunction (OR) circuit in place using mct. Args: self (QuantumCircuit): The QuantumCircuit object to build the disjunction on. qr_variables (QuantumRegister): The QuantumRegister holding the variable qubits. flags (list[int]): A list of +1/-1/0 to mark negations or omissions of qubits. qb_target (Qubit): The target qubit to hold the disjunction result. qr_ancillae (QuantumRegister): The ancillary QuantumRegister for building the mct. mct_mode (str): The mct building mode. """ # pylint: disable=cyclic-import from qiskit.circuit.library import OR warnings.warn( 'The QuantumCircuit.OR method is deprecated as of Terra 0.13.1 / Aqua 0.7.0 and ' 'will be removed no earlier than 3 months after the release date. ' 'The logic OR has moved to qiskit.circuit.library.OR and has become a circuit ' 'object which can be appended to your existing circuit.', DeprecationWarning, stacklevel=2) or_circuit = OR(num_variable_qubits=len(qr_variables), flags=flags, mcx_mode=mct_mode) qubits = qr_variables[:] + [qb_target] if qr_ancillae: qubits += qr_ancillae[:or_circuit.num_ancilla_qubits] self.append(or_circuit.to_gate(), qubits)
def test_or(self, num_variables, flags, mcx_mode): """Test the or circuit.""" or_circuit = OR(num_variables, flags, mcx_mode=mcx_mode) flags = flags or [1] * num_variables def reference(bits): flagged = [] for flag, bit in zip(flags, bits): if flag < 0: flagged += [1 - bit] elif flag > 0: flagged += [bit] return np.any(flagged) self.assertBooleanFunctionIsCorrect(or_circuit, reference)
def construct_circuit(self, circuit=None, variable_register=None, clause_register=None, output_register=None, ancillary_register=None, mct_mode='basic'): # pylint: disable=arguments-differ """ Construct circuit. Args: circuit (QuantumCircuit): The optional circuit to extend from variable_register (QuantumRegister): The optional quantum register to use for problem variables clause_register (QuantumRegister): The optional quantum register to use for problem clauses output_register (QuantumRegister): The optional quantum register to use for holding the output ancillary_register (QuantumRegister): The optional quantum register to use as ancilla mct_mode (str): The mode to use for building Multiple-Control Toffoli Returns: QuantumCircuit: quantum circuit. Raises: AquaError: invalid input """ circuit = self._set_up_circuit(circuit=circuit, variable_register=variable_register, clause_register=clause_register, output_register=output_register, ancillary_register=ancillary_register, mct_mode=mct_mode) if self._depth == 0: self._construct_circuit_for_tiny_expr(circuit) elif self._depth == 1: lits = [l[1] for l in self._ast[1:]] flags = BooleanLogicNormalForm._lits_to_flags(lits) if flags is not None: or_circuit = OR(num_variable_qubits=len( self._variable_register), flags=flags, mcx_mode=mct_mode) qubits = self._variable_register[:] + [ self._output_register[0] ] if self._ancillary_register: qubits += self._ancillary_register[:or_circuit. num_ancilla_qubits] circuit.compose(or_circuit, qubits, inplace=True) else: circuit.u(pi, 0, pi, self._output_register[0]) else: # self._depth == 2 # compute all clauses for clause_index, clause_expr in enumerate(self._ast[1:]): if clause_expr[0] == 'and': lits = [l[1] for l in clause_expr[1:]] elif clause_expr[0] == 'lit': lits = [clause_expr[1]] else: raise AquaError( 'Operator "{}" of clause {} in logic expression {} is unexpected.' .format(clause_expr[0], clause_index, self._ast)) flags = BooleanLogicNormalForm._lits_to_flags(lits) if flags is not None: and_circuit = AND(num_variable_qubits=len( self._variable_register), flags=flags, mcx_mode=mct_mode) qubits = self._variable_register[:] + [ self._clause_register[clause_index] ] if self._ancillary_register: qubits += self._ancillary_register[:and_circuit. num_ancilla_qubits] circuit.compose(and_circuit, qubits, inplace=True) else: circuit.u(pi, 0, pi, self._clause_register[clause_index]) # init the output qubit to 1 circuit.u(pi, 0, pi, self._output_register[self._output_idx]) # collect results from all clauses circuit.u(pi, 0, pi, self._clause_register) circuit.mct(self._clause_register, self._output_register[self._output_idx], self._ancillary_register, mode=mct_mode) circuit.u(pi, 0, pi, self._clause_register) # uncompute all clauses for clause_index, clause_expr in enumerate(self._ast[1:]): if clause_expr[0] == 'and': lits = [l[1] for l in clause_expr[1:]] elif clause_expr[0] == 'lit': lits = [clause_expr[1]] flags = BooleanLogicNormalForm._lits_to_flags(lits) if flags is not None: and_circuit = AND(num_variable_qubits=len( self._variable_register), flags=flags, mcx_mode=mct_mode) qubits = self._variable_register[:] + [ self._clause_register[clause_index] ] if self._ancillary_register: qubits += self._ancillary_register[:and_circuit. num_ancilla_qubits] circuit.compose(and_circuit, qubits, inplace=True) else: circuit.u(pi, 0, pi, self._clause_register[clause_index]) return circuit
def ancillae_thermalisation(theta,n,measure=None,gate_number = False,noise_model = None): register_qubits = QuantumRegister(2 + n*2 ,'r_qbit') target = QuantumRegister(1,'t_qbit') c = ClassicalRegister(1) if n == 0: circ = QuantumCircuit(register_qubits,target,c) circ.append(U(theta),[*register_qubits[:2],target]) else: or_qubits = QuantumRegister(n,'o_qbit') control_qubits = QuantumRegister(1,'c_qbit') circ = QuantumCircuit(register_qubits,or_qubits,control_qubits,target,c) #Initial Round circ.append(U(theta),[*register_qubits[:2],target]) #Subsequent Rounds for i in range(1,n+1): #OR Gate (for 2 register qubits) circ.append(OR(2),[*register_qubits[2*(i-1):2*i],or_qubits[i-1]]) #W Gate circ.cry(np.pi/2,or_qubits[i-1],target) #U Gate circ.append(controlled_U(theta),[or_qubits[i-1],*register_qubits[2*i:2*(i+1)],*control_qubits,target]) if gate_number: dag = circuit_to_dag(circ) pass_ = Unroller(['u3','cx']).run(dag) t_circ = dag_to_circuit(pass_) gate_ops = 0 for instr, _, _ in t_circ: if instr.name not in ['barrier', 'snapshot'] and not instr.params == [0,0,0]: gate_ops += 1 return gate_ops #Pauli Measurements if measure == 'x': circ.h(target) elif measure == 'y': circ.sdg(target) circ.h(target) circ.measure(target,c) shots = 8192 #IBMQ.load_account() #provider = IBMQ.get_provider('ibm-q') if noise_model is None: #qcomp = provider.get_backend('ibmq_16_melbourne') #qcomp = provider.get_backend('ibmq_qasm_simulator') qcomp = Aer.get_backend('qasm_simulator') job = execute(circ, backend = qcomp, shots = shots) else: #qcomp = provider.get_backend('ibmq_qasm_simulator') qcomp = Aer.get_backend('qasm_simulator') q_num = 1 + (2 + 2*n + n + 1) #target, register, or qubits, control qubits noise_model = noise_model(q_num) job = execute(circ, backend = qcomp, shots = shots,noise_model=noise_model,basis_gates=noise_model.basis_gates) #print(job_monitor(job)) result = job.result() result_dictionary = result.get_counts(circ) probs = {} for output in ['0','1']: if output in result_dictionary: probs[output] = result_dictionary[output] else: probs[output] = 0 return (probs['0'] - probs['1']) / shots