本文整理匯總了Python中qiskit.QuantumCircuit.cz方法的典型用法代碼示例。如果您正苦於以下問題:Python QuantumCircuit.cz方法的具體用法?Python QuantumCircuit.cz怎麽用?Python QuantumCircuit.cz使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qiskit.QuantumCircuit
的用法示例。
在下文中一共展示了QuantumCircuit.cz方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: trial_circuit_ryrz
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import cz [as 別名]
def trial_circuit_ryrz(n, m, theta, entangler_map, meas_string=None,
measurement=True):
"""Creates a QuantumCircuit object ocnsisting in layers of
parametrized single-qubit Y and Z rotations and CZ two-qubit gates
Args:
n (int) : number of qubits
m (int) : depth of the circuit
theta array[float] : angles that parametrize the Y and Z rotations
entangler_map : CZ connectivity, e.g. {0: [1], 1: [2]}
meas_string (str) : measure a given Pauli operator at the end of the
circuit
measurement (bool) : whether to measure the qubit (register "q")
on classical bits (register "c")
Returns:
A QuantumCircuit object
"""
q = QuantumRegister("q", n)
c = ClassicalRegister("c", n)
trial_circuit = QuantumCircuit(q, c)
trial_circuit.h(q)
if meas_string is None:
meas_string = [None for x in range(n)]
for i in range(m):
trial_circuit.barrier(q)
for node in entangler_map:
for j in entangler_map[node]:
trial_circuit.cz(q[node], q[j])
for j in range(n):
trial_circuit.ry(theta[n * i * 2 + 2 * j], q[j])
trial_circuit.rz(theta[n * i * 2 + 2 * j + 1], 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
示例2: trial_circuit_ryrz
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import cz [as 別名]
def trial_circuit_ryrz(n, m, theta, entangler_map, meas_string = None, measurement = True):
"""Trial function for classical optimization problems.
n = number of qubits
m = depth
theta = control vector of size n*m*2 stacked as theta[n*i*2+2*j+p] where j
counts the qubits and i the depth and p if y and z.
entangler_map = {0: [2, 1],
1: [2],
3: [2],
4: [2]}
control is the key and values are the target
pauli_string = length of number of qubits string
"""
q = QuantumRegister("q", n)
c = ClassicalRegister("c", n)
trial_circuit = QuantumCircuit(q, c)
trial_circuit.h(q)
if meas_string is None:
meas_string = [None for x in range(n)]
for i in range(m):
trial_circuit.barrier(q)
for node in entangler_map:
for j in entangler_map[node]:
trial_circuit.cz(q[node], q[j])
for j in range(n):
trial_circuit.ry(theta[n * i * 2 + 2*j], q[j])
trial_circuit.rz(theta[n * i * 2 + 2*j + 1], 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
示例3: TestStandard2Q
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import cz [as 別名]
#.........這裏部分代碼省略.........
qasm_txt = 'CX q[0],r[1];\nCX q[1],r[1];\nCX q[2],r[1];'
instruction_set = self.circuit.cx_base(self.q, self.r[1]).inverse()
self.assertStmtsType(instruction_set.instructions, CXBase)
self.assertQasm(qasm_txt)
def test_cxbase_bit_reg(self):
qasm_txt = 'CX q[1],r[0];\nCX q[1],r[1];\nCX q[1],r[2];'
instruction_set = self.circuit.cx_base(self.q[1], self.r)
self.assertStmtsType(instruction_set.instructions, CXBase)
self.assertQasm(qasm_txt)
def test_cxbase_bit_reg_inv(self):
qasm_txt = 'CX q[1],r[0];\nCX q[1],r[1];\nCX q[1],r[2];'
instruction_set = self.circuit.cx_base(self.q[1], self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CXBase)
self.assertQasm(qasm_txt)
def test_cy_reg_reg(self):
qasm_txt = 'cy q[0],r[0];\ncy q[1],r[1];\ncy q[2],r[2];'
instruction_set = self.circuit.cy(self.q, self.r)
self.assertStmtsType(instruction_set.instructions, CyGate)
self.assertQasm(qasm_txt)
def test_cy_reg_reg_inv(self):
qasm_txt = 'cy q[0],r[0];\ncy q[1],r[1];\ncy q[2],r[2];'
instruction_set = self.circuit.cy(self.q, self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CyGate)
self.assertQasm(qasm_txt)
def test_cy_reg_bit(self):
qasm_txt = 'cy q[0],r[1];\ncy q[1],r[1];\ncy q[2],r[1];'
instruction_set = self.circuit.cy(self.q, self.r[1])
self.assertStmtsType(instruction_set.instructions, CyGate)
self.assertQasm(qasm_txt)
def test_cy_reg_bit_inv(self):
qasm_txt = 'cy q[0],r[1];\ncy q[1],r[1];\ncy q[2],r[1];'
instruction_set = self.circuit.cy(self.q, self.r[1]).inverse()
self.assertStmtsType(instruction_set.instructions, CyGate)
self.assertQasm(qasm_txt)
def test_cy_bit_reg(self):
qasm_txt = 'cy q[1],r[0];\ncy q[1],r[1];\ncy q[1],r[2];'
instruction_set = self.circuit.cy(self.q[1], self.r)
self.assertStmtsType(instruction_set.instructions, CyGate)
self.assertQasm(qasm_txt)
def test_cy_bit_reg_inv(self):
qasm_txt = 'cy q[1],r[0];\ncy q[1],r[1];\ncy q[1],r[2];'
instruction_set = self.circuit.cy(self.q[1], self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CyGate)
self.assertQasm(qasm_txt)
def test_cz_reg_reg(self):
qasm_txt = 'cz q[0],r[0];\ncz q[1],r[1];\ncz q[2],r[2];'
instruction_set = self.circuit.cz(self.q, self.r)
self.assertStmtsType(instruction_set.instructions, CzGate)
self.assertQasm(qasm_txt)
def test_cz_reg_reg_inv(self):
qasm_txt = 'cz q[0],r[0];\ncz q[1],r[1];\ncz q[2],r[2];'
instruction_set = self.circuit.cz(self.q, self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CzGate)
self.assertQasm(qasm_txt)
def test_cz_reg_bit(self):
qasm_txt = 'cz q[0],r[1];\ncz q[1],r[1];\ncz q[2],r[1];'
instruction_set = self.circuit.cz(self.q, self.r[1])
self.assertStmtsType(instruction_set.instructions, CzGate)
self.assertQasm(qasm_txt)
def test_cz_reg_bit_inv(self):
qasm_txt = 'cz q[0],r[1];\ncz q[1],r[1];\ncz q[2],r[1];'
instruction_set = self.circuit.cz(self.q, self.r[1]).inverse()
self.assertStmtsType(instruction_set.instructions, CzGate)
self.assertQasm(qasm_txt)
def test_cz_bit_reg(self):
qasm_txt = 'cz q[1],r[0];\ncz q[1],r[1];\ncz q[1],r[2];'
instruction_set = self.circuit.cz(self.q[1], self.r)
self.assertStmtsType(instruction_set.instructions, CzGate)
self.assertQasm(qasm_txt)
def test_cz_bit_reg_inv(self):
qasm_txt = 'cz q[1],r[0];\ncz q[1],r[1];\ncz q[1],r[2];'
instruction_set = self.circuit.cz(self.q[1], self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CzGate)
self.assertQasm(qasm_txt)
def test_swap_reg_reg(self):
qasm_txt = 'swap q[0],r[0];\nswap q[1],r[1];\nswap q[2],r[2];'
instruction_set = self.circuit.swap(self.q, self.r)
self.assertStmtsType(instruction_set.instructions, SwapGate)
self.assertQasm(qasm_txt)
def test_swap_reg_reg_inv(self):
qasm_txt = 'swap q[0],r[0];\nswap q[1],r[1];\nswap q[2],r[2];'
instruction_set = self.circuit.swap(self.q, self.r).inverse()
self.assertStmtsType(instruction_set.instructions, SwapGate)
self.assertQasm(qasm_txt)
示例4: TestStandard1Q
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import cz [as 別名]
#.........這裏部分代碼省略.........
self.assertRaises(QISKitError, c.cx, 0, self.q[0])
self.assertRaises(QISKitError, c.cx, (self.q, 3), self.q[0])
self.assertRaises(QISKitError, c.cx, self.c, self.q)
self.assertRaises(QISKitError, c.cx, 'a', self.q[1])
def test_cxbase(self):
qasm_txt = 'CX q[1],q[2];'
self.circuit.cx_base(self.q[1], self.q[2])
self.assertResult(CXBase, qasm_txt, qasm_txt)
def test_cxbase_invalid(self):
c = self.circuit
self.assertRaises(QISKitError, c.cx_base, self.c[1], self.c[2])
self.assertRaises(QISKitError, c.cx_base, self.q[0], self.q[0])
self.assertRaises(QISKitError, c.cx_base, 0, self.q[0])
self.assertRaises(QISKitError, c.cx_base, (self.q, 3), self.q[0])
self.assertRaises(QISKitError, c.cx_base, self.c, self.q)
self.assertRaises(QISKitError, c.cx_base, 'a', self.q[1])
def test_cy(self):
qasm_txt = 'cy q[1],q[2];'
self.circuit.cy(self.q[1], self.q[2])
self.assertResult(CyGate, qasm_txt, qasm_txt)
def test_cy_invalid(self):
c = self.circuit
self.assertRaises(QISKitError, c.cy, self.c[1], self.c[2])
self.assertRaises(QISKitError, c.cy, self.q[0], self.q[0])
self.assertRaises(QISKitError, c.cy, 0, self.q[0])
self.assertRaises(QISKitError, c.cy, (self.q, 3), self.q[0])
self.assertRaises(QISKitError, c.cy, self.c, self.q)
self.assertRaises(QISKitError, c.cy, 'a', self.q[1])
def test_cz(self):
qasm_txt = 'cz q[1],q[2];'
self.circuit.cz(self.q[1], self.q[2])
self.assertResult(CzGate, qasm_txt, qasm_txt)
def test_cz_invalid(self):
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)
示例5: CircuitBackend
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import cz [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