本文整理汇总了Python中qiskit.QuantumCircuit.cnx方法的典型用法代码示例。如果您正苦于以下问题:Python QuantumCircuit.cnx方法的具体用法?Python QuantumCircuit.cnx怎么用?Python QuantumCircuit.cnx使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qiskit.QuantumCircuit
的用法示例。
在下文中一共展示了QuantumCircuit.cnx方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: len
# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import cnx [as 别名]
elif len(qubits)==3:
qc.ccx(*qubits)
elif len(qubits)==2:
qc.cx(*qubits)
if __name__ == "__main__":
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import CompositeGate, available_backends, execute
q = QuantumRegister(5, "qr")
q2 = QuantumRegister(1, "qr")
print(len(q2))
c = ClassicalRegister(5, "cr")
qc = QuantumCircuit(q, c)
qc.cry = cry
qc.cnx = cnx
qc.any_x = any_x
qc.x_bus = x_bus
qc.bus_or = bus_or
#qc.h(q[0])
qc.h(q[1])
qc.h(q[2])
qc.h(q[3])
qc.h(q[-1])
qc.bus_or(qc,q[0],[q[1],q[2],q[3]],[q[4]])
qc.measure(q,c)
job_sim = execute(qc, "local_qasm_simulator",shots=100)
sim_result = job_sim.result()
示例2: Board
# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import cnx [as 别名]
class Board():
def __init__(self,x,y,print_info=False):
#quantum register, classical register, quantum circuit.
self.print_info=print_info
self.q = QuantumRegister(1)
self.c = ClassicalRegister(1)
self.qc = QuantumCircuit(self.q, self.c)
self.qc.cry = cry
self.qc.x_bus = x_bus
self.qc.cnx = cnx
self.qc.any_x = any_x
self.qc.bus_or = bus_or
#the dimensions of the bord
self.x=x
self.y=y
#To keep track of what is in each cell, no entanglement etc.
#Provides a graphic of the game state.
self.cells = np.empty((x,y),dtype=object)
self.cells[:]='' #Initially game is empty.
self.game_full = False
self.moves = []
def __str__(self):
return str(self.cells)
def add_move(self,indices,player):
"""Adds a move if it is non-clashing, otherwise passes it on"""
for index in indices:
if index[0] >= self.x:
return 'Index out of range'
if index[1] >= self.y:
return 'Index out of range'
status = self._add_move(indices,player)
if status=='ok':
if player==0:
char = 'X'
elif player==1:
char = 'O'
char+=str(len(self.moves))
for index in indices:
s = self.cells[index[0],index[1]]
if s: #If the cell has some text
#Add char with a comma
self.cells[index[0],index[1]]+=' '+char
else: #cell is empty so just add char
self.cells[index[0],index[1]]+=char
print(self.cells)
return status
def _add_move(self,indices,player):
"""Actually adds the move if not clashing,
otherwise passes it to _add_clashing_move"""
if len(indices)==2:
if indices[0]==indices[1]:
indices = [indices[0]]
num=len(indices)
caught_clashes = False #turns true if all moves are safe clashes
for existing_move in self.moves:
for index in indices:
if index in existing_move.indices:
if len(existing_move.indices)==1:
return 'overfull'
#This move will ALWAYS be there, if it can.
#hence, overfull.
else:
#captures any clash
caught_clashes = True
if caught_clashes:
return self._add_clashing_move(indices,player)
else:
#Reach this section if there are no clashes at all
if num==1:
self.moves.append(Move(indices,player)) #No control needed
return 'ok'
else:
self.q.size+=2 #indicator qubit, and move qubit
q1 = self.q[self.q.size-2] #To make this readable...
q2 = self.q[self.q.size-1]
self.qc.h(q1) #the last qubit in register.
self.qc.x(q2)
self.qc.cx(q1,q2)
self.moves.append(Move(indices,player,q1,q2))
return 'ok'
def _add_clashing_move(self,indices,player):
"""Adds a clashing move"""
if len(indices)==1: #100% of qubit is on one clashing spot.
#This spot COULD be occupied.
self.q.size+=1 #Only one bit needed, move happens or not.
index = indices[0]
bus = []
for existing_move in self.moves:
if index in existing_move.indices:
if index==existing_move.indices[0]:
bus.append(existing_move.q1)
elif index==existing_move.indices[1]:
bus.append(existing_move.q2)
#Now if any entry on the bus is true, our qubit is false.
self.qc.x(self.q[self.q.size-1]) # make it 1
self.qc.any_x(self.qc,*bus,self.q[self.q.size-1])
#.........这里部分代码省略.........