本文整理匯總了Python中qiskit.QuantumCircuit.barrier方法的典型用法代碼示例。如果您正苦於以下問題:Python QuantumCircuit.barrier方法的具體用法?Python QuantumCircuit.barrier怎麽用?Python QuantumCircuit.barrier使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qiskit.QuantumCircuit
的用法示例。
在下文中一共展示了QuantumCircuit.barrier方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: qc_approx_sim
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [as 別名]
def qc_approx_sim(x, t1, t2):
theta1 = x - t1;
theta2 = x - t2;
q = QuantumRegister(2, 'q')
c = ClassicalRegister(2, 'c')
qc = QuantumCircuit(q, c)
qc.h( q[0] )
qc.h( q[1] )
qc.u3(t1, 0.0, 0.0, q[0]);
qc.u3(t2, 0.0, 0.0, q[1]);
qc.barrier( q )
#qc.measure(q,c)
qc.measure( q[0], c[0] )
qc.measure( q[1], c[1] )
job = execute(qc, backend, shots=1024)
rslt = job.result()
#counts = rslt.get_counts(qc)
#print(counts)
outputstate = rslt.get_statevector( qc, decimals=13 )
#print(outputstate)
qval = outputstate;
return qval;
示例2: trial_circuit_computational
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [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
示例3: TestStandard3Q
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [as 別名]
class TestStandard3Q(StandardExtensionTest):
"""Standard Extension Test. Gates with three Qubits"""
def setUp(self):
self.q = QuantumRegister(3, "q")
self.r = QuantumRegister(3, "r")
self.s = QuantumRegister(3, "s")
self.c = ClassicalRegister(3, "c")
self.circuit = QuantumCircuit(self.q, self.r, self.s, self.c)
self.c_header = 80 # lenght of the header
def test_barrier_None(self):
self.circuit.barrier()
qasm_txt = 'barrier q[0],q[1],q[2],r[0],r[1],r[2],s[0],s[1],s[2];'
self.assertResult(Barrier, qasm_txt, qasm_txt)
def test_ccx_reg_reg_reg(self):
qasm_txt = 'ccx q[0],r[0],s[0];\nccx q[1],r[1],s[1];\nccx q[2],r[2],s[2];'
instruction_set = self.circuit.ccx(self.q, self.r, self.s)
self.assertStmtsType(instruction_set.instructions, ToffoliGate)
self.assertQasm(qasm_txt)
def test_ccx_reg_reg_inv(self):
qasm_txt = 'ccx q[0],r[0],s[0];\nccx q[1],r[1],s[1];\nccx q[2],r[2],s[2];'
instruction_set = self.circuit.ccx(self.q, self.r, self.s).inverse()
self.assertStmtsType(instruction_set.instructions, ToffoliGate)
self.assertQasm(qasm_txt)
def test_cswap_reg_reg_reg(self):
qasm_txt = 'cswap q[0],r[0],s[0];\n' \
'cswap q[1],r[1],s[1];\n' \
'cswap q[2],r[2],s[2];'
instruction_set = self.circuit.cswap(self.q, self.r, self.s)
self.assertStmtsType(instruction_set.instructions, FredkinGate)
self.assertQasm(qasm_txt)
def test_cswap_reg_reg_inv(self):
qasm_txt = 'cswap q[0],r[0],s[0];\n' \
'cswap q[1],r[1],s[1];\n' \
'cswap q[2],r[2],s[2];'
instruction_set = self.circuit.cswap(self.q, self.r, self.s).inverse()
self.assertStmtsType(instruction_set.instructions, FredkinGate)
self.assertQasm(qasm_txt)
示例4: trial_circuit_ryrz
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [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
示例5: trial_circuit_ryrz
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [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
示例6: TestStandard2Q
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [as 別名]
class TestStandard2Q(StandardExtensionTest):
"""Standard Extension Test. Gates with two Qubits"""
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_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_barrier_reg_bit(self):
self.circuit.barrier(self.q, self.r[0])
qasm_txt = 'barrier q[0],q[1],q[2],r[0];'
self.assertResult(Barrier, qasm_txt, qasm_txt)
def test_ch_reg_reg(self):
qasm_txt = 'ch q[0],r[0];\nch q[1],r[1];\nch q[2],r[2];'
instruction_set = self.circuit.ch(self.q, self.r)
self.assertStmtsType(instruction_set.instructions, CHGate)
self.assertQasm(qasm_txt)
def test_ch_reg_reg_inv(self):
qasm_txt = 'ch q[0],r[0];\nch q[1],r[1];\nch q[2],r[2];'
instruction_set = self.circuit.ch(self.q, self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CHGate)
self.assertQasm(qasm_txt)
def test_ch_reg_bit(self):
qasm_txt = 'ch q[0],r[1];\nch q[1],r[1];\nch q[2],r[1];'
instruction_set = self.circuit.ch(self.q, self.r[1])
self.assertStmtsType(instruction_set.instructions, CHGate)
self.assertQasm(qasm_txt)
def test_ch_reg_bit_inv(self):
qasm_txt = 'ch q[0],r[1];\nch q[1],r[1];\nch q[2],r[1];'
instruction_set = self.circuit.ch(self.q, self.r[1]).inverse()
self.assertStmtsType(instruction_set.instructions, CHGate)
self.assertQasm(qasm_txt)
def test_ch_bit_reg(self):
qasm_txt = 'ch q[1],r[0];\nch q[1],r[1];\nch q[1],r[2];'
instruction_set = self.circuit.ch(self.q[1], self.r)
self.assertStmtsType(instruction_set.instructions, CHGate)
self.assertQasm(qasm_txt)
def test_ch_bit_reg_inv(self):
qasm_txt = 'ch q[1],r[0];\nch q[1],r[1];\nch q[1],r[2];'
instruction_set = self.circuit.ch(self.q[1], self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CHGate)
self.assertQasm(qasm_txt)
def test_crz_reg_reg(self):
qasm_txt = 'crz(1) q[0],r[0];\ncrz(1) q[1],r[1];\ncrz(1) q[2],r[2];'
instruction_set = self.circuit.crz(1, self.q, self.r)
self.assertStmtsType(instruction_set.instructions, CrzGate)
self.assertQasm(qasm_txt)
def test_crz_reg_reg_inv(self):
qasm_txt = 'crz(-1) q[0],r[0];\ncrz(-1) q[1],r[1];\ncrz(-1) q[2],r[2];'
instruction_set = self.circuit.crz(1, self.q, self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CrzGate)
self.assertQasm(qasm_txt)
def test_crz_reg_bit(self):
qasm_txt = 'crz(1) q[0],r[1];\ncrz(1) q[1],r[1];\ncrz(1) q[2],r[1];'
instruction_set = self.circuit.crz(1, self.q, self.r[1])
self.assertStmtsType(instruction_set.instructions, CrzGate)
self.assertQasm(qasm_txt)
def test_crz_reg_bit_inv(self):
qasm_txt = 'crz(-1) q[0],r[1];\ncrz(-1) q[1],r[1];\ncrz(-1) q[2],r[1];'
instruction_set = self.circuit.crz(1, self.q, self.r[1]).inverse()
self.assertStmtsType(instruction_set.instructions, CrzGate)
self.assertQasm(qasm_txt)
def test_crz_bit_reg(self):
qasm_txt = 'crz(1) q[1],r[0];\ncrz(1) q[1],r[1];\ncrz(1) q[1],r[2];'
instruction_set = self.circuit.crz(1, self.q[1], self.r)
self.assertStmtsType(instruction_set.instructions, CrzGate)
self.assertQasm(qasm_txt)
def test_crz_bit_reg_inv(self):
qasm_txt = 'crz(-1) q[1],r[0];\ncrz(-1) q[1],r[1];\ncrz(-1) q[1],r[2];'
instruction_set = self.circuit.crz(1, self.q[1], self.r).inverse()
self.assertStmtsType(instruction_set.instructions, CrzGate)
self.assertQasm(qasm_txt)
def test_cu1_reg_reg(self):
qasm_txt = 'cu1(1) q[0],r[0];\ncu1(1) q[1],r[1];\ncu1(1) q[2],r[2];'
instruction_set = self.circuit.cu1(1, self.q, self.r)
self.assertStmtsType(instruction_set.instructions, Cu1Gate)
self.assertQasm(qasm_txt)
def test_cu1_reg_reg_inv(self):
qasm_txt = 'cu1(-1) q[0],r[0];\ncu1(-1) q[1],r[1];\ncu1(-1) q[2],r[2];'
#.........這裏部分代碼省略.........
示例7: TestStandard1Q
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [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])
#.........這裏部分代碼省略.........
示例8: CircuitBackend
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import barrier [as 別名]
#.........這裏部分代碼省略.........
(theta, phi, lam) = list(map(lambda x: x.sym(nested_scope), arg))
this_gate = self.circuit.u_base(theta, phi, lam,
self._map_qubit(qubit))
if self.creg is not None:
this_gate.c_if(self._map_creg(self.creg), self.cval)
def cx(self, qubit0, qubit1):
"""Fundamental two qubit gate.
qubit0 is (regname,idx) tuple for the control qubit.
qubit1 is (regname,idx) tuple for the target qubit.
"""
if self.listen:
if "CX" not in self.basis:
self.basis.append("CX")
this_gate = self.circuit.cx_base(self._map_qubit(qubit0),
self._map_qubit(qubit1))
if self.creg is not None:
this_gate.c_if(self._map_creg(self.creg), self.cval)
def measure(self, qubit, bit):
"""Measurement operation.
qubit is (regname, idx) tuple for the input qubit.
bit is (regname, idx) tuple for the output bit.
"""
if "measure" not in self.basis:
self.basis.append("measure")
this_op = self.circuit.measure(self._map_qubit(qubit),
self._map_bit(bit))
if self.creg is not None:
this_op.c_if(self._map_creg(self.creg), self.cval)
def barrier(self, qubitlists):
"""Barrier instruction.
qubitlists is a list of lists of (regname, idx) tuples.
"""
if self.listen:
if "barrier" not in self.basis:
self.basis.append("barrier")
flatlist = map(self._map_qubit,
[qubit for qubitlist in qubitlists
for qubit in qubitlist])
self.circuit.barrier(*list(flatlist))
def reset(self, qubit):
"""Reset instruction.
qubit is a (regname, idx) tuple.
"""
if "reset" not in self.basis:
self.basis.append("reset")
this_op = self.circuit.reset(self._map_qubit(qubit))
if self.creg is not None:
this_op.c_if(self._map_creg(self.creg), self.cval)
def set_condition(self, creg, cval):
"""Attach a current condition.
creg is a name string.
cval is the integer value for the test.
"""
self.creg = creg
self.cval = cval