本文整理汇总了Python中qiskit.QuantumCircuit.u2方法的典型用法代码示例。如果您正苦于以下问题:Python QuantumCircuit.u2方法的具体用法?Python QuantumCircuit.u2怎么用?Python QuantumCircuit.u2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qiskit.QuantumCircuit
的用法示例。
在下文中一共展示了QuantumCircuit.u2方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestStandard1Q
# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import u2 [as 别名]
#.........这里部分代码省略.........
self.circuit.u1(1, self.q[1])
self.assertResult(U1Gate, 'u1(1) q[1];', 'u1(-1) q[1];')
def test_u1_invalid(self):
c = self.circuit
# CHECKME? self.assertRaises(QISKitError, c.u1, self.c[0], self.q[0])
self.assertRaises(QISKitError, c.u1, self.c[0], self.c[1])
self.assertRaises(QISKitError, c.u1, self.q[1], 0)
self.assertRaises(QISKitError, c.u1, 0, self.c[0])
self.assertRaises(QISKitError, c.u1, 0, 0)
# TODO self.assertRaises(QISKitError, c.u1, self.q[2], self.q[1])
self.assertRaises(QISKitError, c.u1, 0, (self.q, 3))
self.assertRaises(QISKitError, c.u1, 0, self.c)
# TODO self.assertRaises(QISKitError, c.u1, 'a', self.q[1])
self.assertRaises(QISKitError, c.u1, 0, 'a')
def test_u1_reg(self):
qasm_txt = 'u1(1) q[0];\nu1(1) q[1];\nu1(1) q[2];'
instruction_set = self.circuit.u1(1, self.q)
self.assertStmtsType(instruction_set.instructions, U1Gate)
self.assertQasm(qasm_txt)
def test_u1_reg_inv(self):
qasm_txt = 'u1(-1) q[0];\nu1(-1) q[1];\nu1(-1) q[2];'
instruction_set = self.circuit.u1(1, self.q).inverse()
self.assertStmtsType(instruction_set.instructions, U1Gate)
self.assertQasm(qasm_txt)
def test_u1_pi(self):
c = self.circuit
c.u1(pi / 2, self.q[1])
self.assertResult(U1Gate, 'u1(pi/2) q[1];', 'u1(-pi/2) q[1];')
def test_u2(self):
self.circuit.u2(1, 2, self.q[1])
self.assertResult(U2Gate, 'u2(1,2) q[1];', 'u2(-pi - 2,-1 + pi) q[1];')
def test_u2_invalid(self):
c = self.circuit
# CHECKME? self.assertRaises(QISKitError, c.u2, 0, self.c[0], self.q[0])
self.assertRaises(QISKitError, c.u2, 0, self.c[0], self.c[1])
self.assertRaises(QISKitError, c.u2, 0, self.q[1], 0)
self.assertRaises(QISKitError, c.u2, 0, 0, self.c[0])
self.assertRaises(QISKitError, c.u2, 0, 0, 0)
# TODO self.assertRaises(QISKitError, c.u2, 0, self.q[2], self.q[1])
self.assertRaises(QISKitError, c.u2, 0, 0, (self.q, 3))
self.assertRaises(QISKitError, c.u2, 0, 0, self.c)
# TODO self.assertRaises(QISKitError, c.u2, 0, 'a', self.q[1])
self.assertRaises(QISKitError, c.u2, 0, 0, 'a')
def test_u2_reg(self):
qasm_txt = 'u2(1,2) q[0];\nu2(1,2) q[1];\nu2(1,2) q[2];'
instruction_set = self.circuit.u2(1, 2, self.q)
self.assertStmtsType(instruction_set.instructions, U2Gate)
self.assertQasm(qasm_txt)
def test_u2_reg_inv(self):
qasm_txt = 'u2(-pi - 2,-1 + pi) q[0];\nu2(-pi - 2,-1 + pi) q[1];\nu2(-pi - 2,-1 + pi) q[2];'
instruction_set = self.circuit.u2(1, 2, self.q).inverse()
self.assertStmtsType(instruction_set.instructions, U2Gate)
self.assertQasm(qasm_txt)
def test_u2_pi(self):
c = self.circuit
c.u2(pi / 2, 0.3 * pi, self.q[1])
self.assertResult(U2Gate, 'u2(pi/2,0.3*pi) q[1];', 'u2(-1.3*pi,pi/2) q[1];')
示例2: CircuitBackend
# 需要导入模块: from qiskit import QuantumCircuit [as 别名]
# 或者: from qiskit.QuantumCircuit import u2 [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")
#.........这里部分代码省略.........