本文整理匯總了Python中qiskit.QuantumCircuit.iden方法的典型用法代碼示例。如果您正苦於以下問題:Python QuantumCircuit.iden方法的具體用法?Python QuantumCircuit.iden怎麽用?Python QuantumCircuit.iden使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qiskit.QuantumCircuit
的用法示例。
在下文中一共展示了QuantumCircuit.iden方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestStandard1Q
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import iden [as 別名]
#.........這裏部分代碼省略.........
c = self.circuit
self.assertRaises(QISKitError, c.cz, self.c[1], self.c[2])
self.assertRaises(QISKitError, c.cz, self.q[0], self.q[0])
self.assertRaises(QISKitError, c.cz, 0, self.q[0])
self.assertRaises(QISKitError, c.cz, (self.q, 3), self.q[0])
self.assertRaises(QISKitError, c.cz, self.c, self.q)
self.assertRaises(QISKitError, c.cz, 'a', self.q[1])
def test_h(self):
qasm_txt = 'h q[1];'
self.circuit.h(self.q[1])
self.assertResult(HGate, qasm_txt, qasm_txt)
def test_h_invalid(self):
c = self.circuit
self.assertRaises(QISKitError, c.h, self.c[0])
self.assertRaises(QISKitError, c.h, self.c)
self.assertRaises(QISKitError, c.h, (self.q, 3))
self.assertRaises(QISKitError, c.h, (self.q, 'a'))
self.assertRaises(QISKitError, c.h, 0)
def test_h_reg(self):
qasm_txt = 'h q[0];\nh q[1];\nh q[2];'
instruction_set = self.circuit.h(self.q)
self.assertStmtsType(instruction_set.instructions, HGate)
self.assertQasm(qasm_txt)
def test_h_reg_inv(self):
qasm_txt = 'h q[0];\nh q[1];\nh q[2];'
instruction_set = self.circuit.h(self.q).inverse()
self.assertStmtsType(instruction_set.instructions, HGate)
self.assertQasm(qasm_txt, offset=len(qasm_txt) - 22)
def test_iden(self):
self.circuit.iden(self.q[1])
self.assertResult(IdGate, 'id q[1];', 'id q[1];')
def test_iden_invalid(self):
c = self.circuit
self.assertRaises(QISKitError, c.iden, self.c[0])
self.assertRaises(QISKitError, c.iden, self.c)
self.assertRaises(QISKitError, c.iden, (self.q, 3))
self.assertRaises(QISKitError, c.iden, (self.q, 'a'))
self.assertRaises(QISKitError, c.iden, 0)
def test_iden_reg(self):
qasm_txt = 'id q[0];\nid q[1];\nid q[2];'
instruction_set = self.circuit.iden(self.q)
self.assertStmtsType(instruction_set.instructions, IdGate)
self.assertQasm(qasm_txt)
def test_iden_reg_inv(self):
qasm_txt = 'id q[0];\nid q[1];\nid q[2];'
instruction_set = self.circuit.iden(self.q).inverse()
self.assertStmtsType(instruction_set.instructions, IdGate)
self.assertQasm(qasm_txt, offset=len(qasm_txt) - 25)
def test_rx(self):
self.circuit.rx(1, self.q[1])
self.assertResult(RXGate, 'rx(1) q[1];', 'rx(-1) q[1];')
def test_rx_invalid(self):
c = self.circuit
self.assertRaises(QISKitError, c.rx, self.c[0], self.c[1])
self.assertRaises(QISKitError, c.rx, self.q[1], 0)
self.assertRaises(QISKitError, c.rx, 0, self.c[0])
示例2: CircuitBackend
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import iden [as 別名]
#.........這裏部分代碼省略.........
creg is a name string.
cval is the integer value for the test.
"""
self.creg = creg
self.cval = cval
def drop_condition(self):
"""Drop the current condition."""
self.creg = None
self.cval = None
def start_gate(self, name, args, qubits, nested_scope=None):
"""Begin a custom gate.
name is name string.
args is list of Node expression objects.
qubits is list of (regname, idx) tuples.
nested_scope is a list of dictionaries mapping expression variables
to Node expression objects in order of increasing nesting depth.
"""
if self.listen and name not in self.basis \
and self.gates[name]["opaque"]:
raise BackendError("opaque gate %s not in basis" % name)
if self.listen and name in self.basis:
self.in_gate = name
self.listen = False
# Gate names mapped to number of arguments and qubits
# and method to invoke on [args, qubits]
lut = {"ccx": [(0, 3),
lambda x: self.circuit.ccx(x[1][0], x[1][1],
x[1][2])],
"ch": [(0, 2),
lambda x: self.circuit.ch(x[1][0], x[1][1])],
"crz": [(1, 2),
lambda x: self.circuit.crz(x[0][0], x[1][0],
x[1][1])],
"cswap": [(0, 3),
lambda x: self.circuit.cswap(x[1][0],
x[1][1],
x[1][2])],
"cu1": [(1, 2),
lambda x: self.circuit.cu1(x[0][0], x[1][0],
x[1][1])],
"cu3": [(3, 2), lambda x: self.circuit.cu3(x[0][0],
x[0][1],
x[0][2],
x[1][0],
x[1][1])],
"cx": [(0, 2), lambda x: self.circuit.cx(x[1][0], x[1][1])],
"cy": [(0, 2), lambda x: self.circuit.cy(x[1][0], x[1][1])],
"cz": [(0, 2), lambda x: self.circuit.cz(x[1][0], x[1][1])],
"swap": [(0, 2), lambda x: self.circuit.swap(x[1][0], x[1][1])],
"h": [(0, 1), lambda x: self.circuit.h(x[1][0])],
"id": [(0, 1), lambda x: self.circuit.iden(x[1][0])],
"rx": [(1, 1), lambda x: self.circuit.rx(x[0][0], x[1][0])],
"ry": [(1, 1), lambda x: self.circuit.ry(x[0][0], x[1][0])],
"rz": [(1, 1), lambda x: self.circuit.rz(x[0][0], x[1][0])],
"s": [(0, 1), lambda x: self.circuit.s(x[1][0])],
"sdg": [(0, 1), lambda x: self.circuit.s(x[1][0]).inverse()],
"t": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],
"tdg": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],
"u1": [(1, 1), lambda x: self.circuit.u1(x[0][0], x[1][0])],
"u2": [(2, 1), lambda x: self.circuit.u2(x[0][0], x[0][1],
x[1][0])],
"u3": [(3, 1), lambda x: self.circuit.u3(x[0][0], x[0][1],
x[0][2], x[1][0])],
"x": [(0, 1), lambda x: self.circuit.x(x[1][0])],
"y": [(0, 1), lambda x: self.circuit.y(x[1][0])],
"z": [(0, 1), lambda x: self.circuit.z(x[1][0])]}
if name not in lut:
raise BackendError("gate %s not in standard extensions" %
name)
gate_data = lut[name]
if gate_data[0] != (len(args), len(qubits)):
raise BackendError("gate %s signature (%d, %d) is " %
(name, len(args), len(qubits)) +
"incompatible with the standard " +
"extensions")
this_gate = gate_data[1]([list(map(lambda x:
x.sym(nested_scope), args)),
list(map(self._map_qubit, qubits))])
if self.creg is not None:
this_gate.c_if(self._map_creg(self.creg), self.cval)
def end_gate(self, name, args, qubits, nested_scope=None):
"""End a custom gate.
name is name string.
args is list of Node expression objects.
qubits is list of (regname, idx) tuples.
nested_scope is a list of dictionaries mapping expression variables
to Node expression objects in order of increasing nesting depth.
"""
if name == self.in_gate:
self.in_gate = ""
self.listen = True
def get_output(self):
"""Return the QuantumCircuit object."""
return self.circuit