当前位置: 首页>>代码示例>>Python>>正文


Python QuantumCircuit.x方法代码示例

本文整理汇总了Python中qiskit.QuantumCircuit.x方法的典型用法代码示例。如果您正苦于以下问题:Python QuantumCircuit.x方法的具体用法?Python QuantumCircuit.x怎么用?Python QuantumCircuit.x使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在qiskit.QuantumCircuit的用法示例。


在下文中一共展示了QuantumCircuit.x方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: trial_circuit_computational

# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import x [as 别名]
def trial_circuit_computational(n, state, meas_string = None, measurement = True):
    """Trial function for classical optimization problems.

    n = number of qubits
    state = a bit string for the state prepared.
    meas_string = the pauli to be measured
    measurement = true/false if measurement is to be done
    """
    q = QuantumRegister("q", n)
    c = ClassicalRegister("c", n)
    trial_circuit = QuantumCircuit(q, c)
    if meas_string is None:
        meas_string = [None for x in range(n)]
    if len(state) == n:
        for j in range(n):
            if state[n-j-1] == "1":
                trial_circuit.x(q[j])
        trial_circuit.barrier(q)
        for j in range(n):
            if meas_string[j] == 'X':
                trial_circuit.h(q[j])
            elif meas_string[j] == 'Y':
                trial_circuit.s(q[j]).inverse()
                trial_circuit.h(q[j])
        if measurement:
            for j in range(n):
                trial_circuit.measure(q[j], c[j])
    return trial_circuit
开发者ID:SKRohit,项目名称:The_Math_of_Intelligence,代码行数:30,代码来源:optimization.py

示例2: test_gate_x

# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import x [as 别名]
 def test_gate_x(self):
     shots = 100
     qr = QuantumRegister(1)
     cr = ClassicalRegister(1)
     qc = QuantumCircuit(qr, cr, name='test_gate_x')
     qc.x(qr[0])
     qc.measure(qr, cr)
     qobj = qiskit._compiler.compile([qc], pq_simulator, shots=shots)
     q_job = QuantumJob(qobj, pq_simulator, preformatted=True,
                        resources={'max_credits': qobj['config']['max_credits']})
     job = pq_simulator.run(q_job)
     result_pq = job.result(timeout=30)
     self.assertEqual(result_pq.get_counts(result_pq.get_names()[0]),
                      {'1': shots})
开发者ID:christians94,项目名称:qiskit-sdk-py,代码行数:16,代码来源:test_qasm_simulator_projectq.py

示例3: TestStandard1Q

# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import x [as 别名]
class TestStandard1Q(StandardExtensionTest):
    """Standard Extension Test. Gates with a single Qubit"""

    def setUp(self):
        self.q = QuantumRegister(3, "q")
        self.r = QuantumRegister(3, "r")
        self.c = ClassicalRegister(3, "c")
        self.circuit = QuantumCircuit(self.q, self.r, self.c)
        self.c_header = 69  # lenght of the header

    def test_barrier(self):
        self.circuit.barrier(self.q[1])
        qasm_txt = 'barrier q[1];'
        self.assertResult(Barrier, qasm_txt, qasm_txt)

    def test_barrier_invalid(self):
        c = self.circuit
        self.assertRaises(QISKitError, c.barrier, self.c[0])
        self.assertRaises(QISKitError, c.barrier, self.c)
        self.assertRaises(QISKitError, c.barrier, (self.q, 3))
        self.assertRaises(QISKitError, c.barrier, (self.q, 'a'))
        self.assertRaises(QISKitError, c.barrier, 0)

    def test_barrier_reg(self):
        self.circuit.barrier(self.q)
        qasm_txt = 'barrier q[0],q[1],q[2];'
        self.assertResult(Barrier, qasm_txt, qasm_txt)

    def test_barrier_None(self):
        self.circuit.barrier()
        qasm_txt = 'barrier q[0],q[1],q[2],r[0],r[1],r[2];'
        self.assertResult(Barrier, qasm_txt, qasm_txt)

    def test_ccx(self):
        self.circuit.ccx(self.q[0], self.q[1], self.q[2])
        qasm_txt = 'ccx q[0],q[1],q[2];'
        self.assertResult(ToffoliGate, qasm_txt, qasm_txt)

    def test_ccx_invalid(self):
        c = self.circuit
        self.assertRaises(QISKitError, c.ccx, self.c[0], self.c[1], self.c[2])
        self.assertRaises(QISKitError, c.ccx, self.q[0], self.q[0], self.q[2])
        self.assertRaises(QISKitError, c.ccx, 0, self.q[0], self.q[2])
        self.assertRaises(QISKitError, c.ccx, (self.q, 3), self.q[1], self.q[2])
        self.assertRaises(QISKitError, c.ccx, self.c, self.q, self.q)
        self.assertRaises(QISKitError, c.ccx, 'a', self.q[1], self.q[2])

    def test_ch(self):
        self.circuit.ch(self.q[0], self.q[1])
        qasm_txt = 'ch q[0],q[1];'
        self.assertResult(CHGate, qasm_txt, qasm_txt)

    def test_ch_invalid(self):
        c = self.circuit
        self.assertRaises(QISKitError, c.ch, self.c[0], self.c[1])
        self.assertRaises(QISKitError, c.ch, self.q[0], self.q[0])
        self.assertRaises(QISKitError, c.ch, 0, self.q[0])
        self.assertRaises(QISKitError, c.ch, (self.q, 3), self.q[0])
        self.assertRaises(QISKitError, c.ch, self.c, self.q)
        self.assertRaises(QISKitError, c.ch, 'a', self.q[1])

    def test_crz(self):
        self.circuit.crz(1, self.q[0], self.q[1])
        self.assertResult(CrzGate, 'crz(1) q[0],q[1];', 'crz(-1) q[0],q[1];')

    def test_crz_invalid(self):
        c = self.circuit
        self.assertRaises(QISKitError, c.crz, 0, self.c[0], self.c[1])
        self.assertRaises(QISKitError, c.crz, 0, self.q[0], self.q[0])
        self.assertRaises(QISKitError, c.crz, 0, 0, self.q[0])
        # TODO self.assertRaises(QISKitError, c.crz, self.q[2], self.q[1], self.q[0])
        self.assertRaises(QISKitError, c.crz, 0, self.q[1], self.c[2])
        self.assertRaises(QISKitError, c.crz, 0, (self.q, 3), self.q[1])
        self.assertRaises(QISKitError, c.crz, 0, self.c, self.q)
        # TODO self.assertRaises(QISKitError, c.crz, 'a', self.q[1], self.q[2])

    def test_cswap(self):
        self.circuit.cswap(self.q[0], self.q[1], self.q[2])
        qasm_txt = 'cx q[2],q[1];\nccx q[0],q[1],q[2];\ncx q[2],q[1];'
        self.assertResult(FredkinGate, qasm_txt, qasm_txt)

    def test_cswap_invalid(self):
        c = self.circuit
        self.assertRaises(QISKitError, c.cswap, self.c[0], self.c[1], self.c[2])
        self.assertRaises(QISKitError, c.cswap, self.q[1], self.q[0], self.q[0])
        self.assertRaises(QISKitError, c.cswap, self.q[1], 0, self.q[0])
        self.assertRaises(QISKitError, c.cswap, self.c[0], self.c[1], self.q[0])
        self.assertRaises(QISKitError, c.cswap, self.q[0], self.q[0], self.q[1])
        self.assertRaises(QISKitError, c.cswap, 0, self.q[0], self.q[1])
        self.assertRaises(QISKitError, c.cswap, (self.q, 3), self.q[0], self.q[1])
        self.assertRaises(QISKitError, c.cswap, self.c, self.q[0], self.q[1])
        self.assertRaises(QISKitError, c.cswap, 'a', self.q[1], self.q[2])

    def test_cu1(self):
        self.circuit.cu1(1, self.q[1], self.q[2])
        self.assertResult(Cu1Gate, 'cu1(1) q[1],q[2];', 'cu1(-1) q[1],q[2];')

    def test_cu1_invalid(self):
        c = self.circuit
        self.assertRaises(QISKitError, c.cu1, self.c[0], self.c[1], self.c[2])
#.........这里部分代码省略.........
开发者ID:christians94,项目名称:qiskit-sdk-py,代码行数:103,代码来源:test_extensions_standard.py

示例4: CircuitBackend

# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import x [as 别名]
class CircuitBackend(UnrollerBackend):
    """Backend for the unroller that produces a QuantumCircuit.

    By default, basis gates are the QX gates.
    """

    def __init__(self, basis=None):
        """Setup this backend.

        basis is a list of operation name strings.
        """
        super().__init__(basis)
        self.creg = None
        self.cval = None
        if basis:
            self.basis = basis
        else:
            self.basis = ["cx", "u1", "u2", "u3"]
        self.gates = {}
        self.listen = True
        self.in_gate = ""
        self.circuit = QuantumCircuit()

    def set_basis(self, basis):
        """Declare the set of user-defined gates to emit.

        basis is a list of operation name strings.
        """
        self.basis = basis

    def version(self, version):
        """Ignore the version string.

        v is a version number.
        """
        pass

    def new_qreg(self, name, size):
        """Create a new quantum register.

        name = name of the register
        sz = size of the register
        """
        assert size >= 0, "invalid qreg size"
        q_register = QuantumRegister(size, name)
        self.circuit.add(q_register)

    def new_creg(self, name, size):
        """Create a new classical register.

        name = name of the register
        sz = size of the register
        """
        assert size >= 0, "invalid creg size"
        c_register = ClassicalRegister(size, name)
        self.circuit.add(c_register)

    def define_gate(self, name, gatedata):
        """Define a new quantum gate.

        We don't check that the definition and name agree.

        name is a string.
        gatedata is the AST node for the gate.
        """
        self.gates[name] = gatedata

    def _map_qubit(self, qubit):
        """Map qubit tuple (regname, index) to (QuantumRegister, index)."""
        qregs = self.circuit.get_qregs()
        if qubit[0] not in qregs:
            raise BackendError("qreg %s does not exist" % qubit[0])
        return (qregs[qubit[0]], qubit[1])

    def _map_bit(self, bit):
        """Map bit tuple (regname, index) to (ClassicalRegister, index)."""
        cregs = self.circuit.get_cregs()
        if bit[0] not in cregs:
            raise BackendError("creg %s does not exist" % bit[0])
        return (cregs[bit[0]], bit[1])

    def _map_creg(self, creg):
        """Map creg name to ClassicalRegister."""
        cregs = self.circuit.get_cregs()
        if creg not in cregs:
            raise BackendError("creg %s does not exist" % creg)
        return cregs[creg]

    def u(self, arg, qubit, nested_scope=None):
        """Fundamental single qubit gate.

        arg is 3-tuple of Node expression objects.
        qubit is (regname,idx) tuple.
        nested_scope is a list of dictionaries mapping expression variables
        to Node expression objects in order of increasing nesting depth.
        """
        if self.listen:
            if "U" not in self.basis:
                self.basis.append("U")

#.........这里部分代码省略.........
开发者ID:christians94,项目名称:qiskit-sdk-py,代码行数:103,代码来源:_circuitbackend.py

示例5: Board

# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import x [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])
#.........这里部分代码省略.........
开发者ID:GiuseppeOrlando878776,项目名称:qiskit-tutorials,代码行数:103,代码来源:q_tic_tac_toe.py


注:本文中的qiskit.QuantumCircuit.x方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。