本文整理匯總了Python中qiskit.QuantumCircuit.h方法的典型用法代碼示例。如果您正苦於以下問題:Python QuantumCircuit.h方法的具體用法?Python QuantumCircuit.h怎麽用?Python QuantumCircuit.h使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qiskit.QuantumCircuit
的用法示例。
在下文中一共展示了QuantumCircuit.h方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: trial_circuit_computational
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [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
示例2: test_two_unitary_simulator
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def test_two_unitary_simulator(self):
"""test running two circuits
This test is similar to one in test_quantumprogram but doesn't use
multiprocessing.
"""
qr = QuantumRegister(2)
cr = ClassicalRegister(1)
qc1 = QuantumCircuit(qr, cr)
qc2 = QuantumCircuit(qr, cr)
qc1.h(qr)
qc2.cx(qr[0], qr[1])
backend = UnitarySimulatorPy()
qobj = compile([qc1, qc2], backend=backend)
job = backend.run(QuantumJob(qobj, backend=backend, preformatted=True))
unitary1 = job.result().get_unitary(qc1)
unitary2 = job.result().get_unitary(qc2)
unitaryreal1 = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, -0.5],
[0.5, 0.5, -0.5, -0.5],
[0.5, -0.5, -0.5, 0.5]])
unitaryreal2 = np.array([[1, 0, 0, 0], [0, 0, 0, 1],
[0., 0, 1, 0], [0, 1, 0, 0]])
norm1 = np.trace(np.dot(np.transpose(np.conj(unitaryreal1)), unitary1))
norm2 = np.trace(np.dot(np.transpose(np.conj(unitaryreal2)), unitary2))
self.assertAlmostEqual(norm1, 4)
self.assertAlmostEqual(norm2, 4)
示例3: qc_approx_sim
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [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;
示例4: setUp
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def setUp(self):
qr = QuantumRegister(2, name="qr2")
cr = ClassicalRegister(2, name=None)
qc = QuantumCircuit(qr, cr, name="qc10")
qc.h(qr[0])
qc.measure(qr[0], cr[0])
self.qr_name = qr.name
self.cr_name = cr.name
self.circuits = [qc]
示例5: setUpClass
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def setUpClass(cls, QE_TOKEN, QE_URL, hub=None, group=None, project=None):
# pylint: disable=arguments-differ
super().setUpClass()
# create QuantumCircuit
qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(2, 'c')
qc = QuantumCircuit(qr, cr)
qc.h(qr[0])
qc.cx(qr[0], qr[1])
qc.measure(qr, cr)
cls._qc = qc
cls._provider = LocalProvider(QE_TOKEN, QE_URL, hub, group, project)
示例6: setUp
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def setUp(self):
self.seed = 88
self.qasm_filename = self._get_resource_path('qasm/example.qasm')
with open(self.qasm_filename, 'r') as qasm_file:
self.qasm_text = qasm_file.read()
self.qasm_ast = qiskit.qasm.Qasm(data=self.qasm_text).parse()
self.qasm_be = qiskit.unroll.CircuitBackend(['u1', 'u2', 'u3', 'id', 'cx'])
self.qasm_circ = qiskit.unroll.Unroller(self.qasm_ast, self.qasm_be).execute()
qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(2, 'c')
qc = QuantumCircuit(qr, cr)
qc.h(qr[0])
qc.measure(qr[0], cr[0])
self.qc = qc
# create qobj
compiled_circuit1 = qiskit._compiler.compile_circuit(self.qc, format='json')
compiled_circuit2 = qiskit._compiler.compile_circuit(self.qasm_circ, format='json')
self.qobj = {'id': 'test_qobj',
'config': {
'max_credits': 3,
'shots': 2000,
'backend_name': 'local_qasm_simulator_cpp',
'seed': 1111
},
'circuits': [
{
'name': 'test_circuit1',
'compiled_circuit': compiled_circuit1,
'basis_gates': 'u1,u2,u3,cx,id',
'layout': None,
},
{
'name': 'test_circuit2',
'compiled_circuit': compiled_circuit2,
'basis_gates': 'u1,u2,u3,cx,id',
'layout': None,
}
]}
# Simulator backend
try:
self.backend = QasmSimulatorCpp()
except FileNotFoundError as fnferr:
raise unittest.SkipTest(
'cannot find {} in path'.format(fnferr))
self.q_job = QuantumJob(self.qobj,
backend=self.backend,
preformatted=True)
示例7: test_initialize_middle_circuit
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def test_initialize_middle_circuit(self):
desired_vector = [0.5, 0.5, 0.5, 0.5]
qr = QuantumRegister(2, "qr")
cr = ClassicalRegister(2, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(qr[0])
qc.cx(qr[0], qr[1])
qc.reset(qr[0])
qc.reset(qr[1])
qc.initialize(desired_vector, [qr[0], qr[1]])
qc.measure(qr, cr)
# statevector simulator does not support reset
shots = 2000
threshold = 0.04 * shots
job = wrapper.execute(qc, 'local_qasm_simulator', shots=shots)
result = job.result()
counts = result.get_counts()
target = {'00': shots / 4, '01': shots / 4, '10': shots / 4, '11': shots / 4}
self.assertDictAlmostEqual(counts, target, threshold)
示例8: setUp
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def setUp(self):
self.seed = 88
self.qasmFileName = os.path.join(qiskit.__path__[0],
'../test/python/qasm/example.qasm')
with open(self.qasmFileName, 'r') as qasm_file:
self.qasm_text = qasm_file.read()
qr = QuantumRegister('q', 2)
cr = ClassicalRegister('c', 2)
qc = QuantumCircuit(qr, cr)
qc.h(qr[0])
qc.measure(qr[0], cr[0])
self.qc = qc
# create qobj
compiled_circuit1 = openquantumcompiler.compile(self.qc.qasm(),
format='json')
compiled_circuit2 = openquantumcompiler.compile(self.qasm_text,
format='json')
self.qobj = {'id': 'test_qobj',
'config': {
'max_credits': 3,
'shots': 100,
'backend': 'local_qasm_simulator',
'seed': 1111
},
'circuits': [
{
'name': 'test_circuit1',
'compiled_circuit': compiled_circuit1,
'basis_gates': 'u1,u2,u3,cx,id',
'layout': None,
},
{
'name': 'test_circuit2',
'compiled_circuit': compiled_circuit2,
'basis_gates': 'u1,u2,u3,cx,id',
'layout': None,
}
]
}
self.q_job = QuantumJob(self.qobj,
backend='local_qasm_cpp_simulator',
preformatted=True)
示例9: test_entangle
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def test_entangle(self):
shots = 100
N = 5
qr = QuantumRegister(N)
cr = ClassicalRegister(N)
qc = QuantumCircuit(qr, cr, name='test_entangle')
qc.h(qr[0])
for i in range(1, N):
qc.cx(qr[0], qr[i])
qc.measure(qr, cr)
qobj = qiskit._compiler.compile([qc], pq_simulator, shots=shots)
timeout = 30
q_job = QuantumJob(qobj, pq_simulator, preformatted=True,
resources={'max_credits': qobj['config']['max_credits']})
job = pq_simulator.run(q_job)
result = job.result(timeout=timeout)
counts = result.get_counts(result.get_names()[0])
self.log.info(counts)
for key, _ in counts.items():
with self.subTest(key=key):
self.assertTrue(key in ['0' * N, '1' * N])
示例10: setUp
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
def setUp(self):
self.seed = 88
self.qasmFileName = self._get_resource_path('qasm/example.qasm')
with open(self.qasmFileName, 'r') as qasm_file:
self.qasm_text = qasm_file.read()
# create QuantumCircuit
qr = QuantumRegister('q', 2)
cr = ClassicalRegister('c', 2)
qc = QuantumCircuit(qr, cr)
qc.h(qr[0])
qc.measure(qr[0], cr[0])
self.qc = qc
# create qobj
compiled_circuit1 = openquantumcompiler.compile(self.qc.qasm())
compiled_circuit2 = openquantumcompiler.compile(self.qasm_text)
self.qobj = {'id': 'test_qobj',
'config': {
'max_credits': 3,
'shots': 100,
'backend': 'local_qasm_simulator',
},
'circuits': [
{
'name': 'test_circuit1',
'compiled_circuit': compiled_circuit1,
'basis_gates': 'u1,u2,u3,cx,id',
'layout': None,
'seed': None
},
{
'name': 'test_circuit2',
'compiled_circuit': compiled_circuit2,
'basis_gates': 'u1,u2,u3,cx,id',
'layout': None,
'seed': None
}
]
}
示例11: trial_circuit_ryrz
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [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
示例12: trial_circuit_ryrz
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [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
示例13: TestStandard1Q
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [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])
#.........這裏部分代碼省略.........
示例14: QuantumRegister
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [as 別名]
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import CompositeGate, available_backends, execute
q = QuantumRegister(5, "qr")
q2 = QuantumRegister(1, "qr")
print(len(q2))
c = ClassicalRegister(5, "cr")
qc = QuantumCircuit(q, c)
qc.cry = cry
qc.cnx = cnx
qc.any_x = any_x
qc.x_bus = x_bus
qc.bus_or = bus_or
#qc.h(q[0])
qc.h(q[1])
qc.h(q[2])
qc.h(q[3])
qc.h(q[-1])
qc.bus_or(qc,q[0],[q[1],q[2],q[3]],[q[4]])
qc.measure(q,c)
job_sim = execute(qc, "local_qasm_simulator",shots=100)
sim_result = job_sim.result()
# Show the results
print("simulation: ", sim_result)
print(sim_result.get_counts(qc))
print(qc.qasm())
示例15: CircuitBackend
# 需要導入模塊: from qiskit import QuantumCircuit [as 別名]
# 或者: from qiskit.QuantumCircuit import h [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")
#.........這裏部分代碼省略.........