def test_dump_values(self): qregs = new_qregs(4) cregs = new_references(4) circuit = [I(qreg) for qreg in qregs] sim = qgate.simulator.py() sim.run(circuit) qgate.dump(sim.values, file=self.file)
def test_dump_qubits(self): qregs = new_qregs(4) circuit = [H(qreg) for qreg in qregs] sim = qgate.simulator.py() sim.run(circuit) qgate.dump(sim.qubits.states, file=self.file) qgate.dump(sim.qubits.prob, file=self.file)
def test_observation_list_repr(self): qregs = new_qregs(4) cregs = new_references(4) circuit = [ [H(qreg) for qreg in qregs], measure(cregs[0], qregs[0]), measure(cregs[2], qregs[2]), measure(cregs[3], qregs[3]), ] sim = qgate.simulator.py() obslist = sim.sample(circuit, cregs, 128) qgate.dump(obslist, file=self.file) print(obslist.__repr__(), file=self.file)
def run(caption, circuit, refs = None) : prefs = { qgate.prefs.circuit_prep: qgate.prefs.dynamic } # sim = qgate.simulator.py(**prefs) sim = qgate.simulator.cpu(**prefs) # sim = qgate.simulator.cuda(**prefs) sim.run(circuit) print(caption) qgate.dump(sim.qubits) qgate.dump(sim.qubits.prob) if refs is not None : print('observation: {}'.format(sim.obs(refs))) print() sim.terminate()
def run(circuit, caption) : sim = qgate.simulator.cuda(dtype=np.float32, isolate_circuits = False) n_lanes_per_device = -1 device_ids = [] if this.mgpu : n_lanes_per_device = this.n_qubits - 1 device_ids = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] sim.run(circuit) print(caption) states = sim.qubits.get_states() qgate.dump(sim.values) print() sim.terminate()
def run(circuit, caption): prefs = {'isolate_circuits': True} # sim = qgate.simulator.py(**prefs) sim = qgate.simulator.cpu(**prefs) # sim = qgate.simulator.cuda(**prefs) sim.run(circuit) print(caption) qgate.dump(sim.qubits, qgate.simulator.prob) qgate.dump(sim.qubits) qgate.dump(sim.values) print() sim.terminate()
# allocating qubit register qregs = new_qregs(2) q0, q1 = qregs[0], qregs[1] # building a quantum circuit. circuit = [[H(qreg) for qreg in qregs], H(q1), ctrl(q0).X(q1), H(q1), [H(qreg) for qreg in qregs], [X(qreg) for qreg in qregs], H(q1), ctrl(q0).X(q1), H(q1), [X(qreg) for qreg in qregs], [H(qreg) for qreg in qregs]] # measure refs = new_references(2) circuit += [measure(refs[0], qregs[0]), measure(refs[1], qregs[1])] sim = qgate.simulator.py() sim.run(circuit) results = sim.values.get(refs) print('Results\n' '(q0, q1) = ({}, {})'.format(results[0], results[1])) print('\ndump results:') qgate.dump(sim.values) print('\ndump state vector:') qgate.dump(sim.qubits) sim.terminate()
circuit = [ X(q[0]), # x q[0] X(q[2]), # x q[2]; barrier(q), # barrier q; H(q[0]), # h q[0]; ctrl(q[1]).U1(math.pi / 2.)(q[0]), # cu1(pi/2) q[1],q[0]; H(q[1]), # h q[1]; ctrl(q[2]).U1(math.pi / 4.)(q[0]), # cu1(pi/4) q[2],q[0]; ctrl(q[2]).U1(math.pi / 2.)(q[1]), # cu1(pi/2) q[2],q[1]; H(q[2]), # h q[2]; ctrl(q[3]).U1(math.pi / 8.)(q[0]), # cu1(pi/8) q[3],q[0]; ctrl(q[3]).U1(math.pi / 4.)(q[1]), # cu1(pi/4) q[3],q[1]; ctrl(q[3]).U1(math.pi / 2.)(q[2]), # cu1(pi/2) q[3],q[2]; H(q[3]), # h q[3]; ] sim = qgate.simulator.py() sim.run(circuit) print('\nqubit states') qgate.dump(sim.qubits) print('\nprobability') qgate.dump(sim.qubits.prob) sim.run([measure(_c, _q) for _c, _q in zip(c, q)]) # measure q -> c; obs = sim.obs(c) print('\nobservation: {}\n'.format(repr(obs))) sim.terminate()
# Glover's algorithm # allocating qubit register qregs = new_qregs(2) q0, q1 = qregs[0], qregs[1] # building a quantum circuit. circuit = [[H(qreg) for qreg in qregs], H(q1), ctrl(q0).X(q1), H(q1), [H(qreg) for qreg in qregs], [X(qreg) for qreg in qregs], H(q1), ctrl(q0).X(q1), H(q1), [X(qreg) for qreg in qregs], [H(qreg) for qreg in qregs]] # measure refs = new_references(2) circuit += [measure(refs[0], qregs[0]), measure(refs[1], qregs[1])] sim = qgate.simulator.py() sim.run(circuit) obs = sim.obs(refs) print('observation\n' '(c0, c1) = ({}, {})'.format(obs(refs[0]), obs(refs[1]))) print(obs) print('\ndump state vector:') qgate.dump(sim.qubits) sim.terminate()
def test_dump_circuit(self): qreg0, qreg1 = new_qregs(2) circuit = [ I(qreg0), H(qreg0), S(qreg0), T(qreg0), X(qreg0), Y(qreg0), Z(qreg0), Rx(0.)(qreg0), Ry(0.)(qreg0), Rz(0.)(qreg0), U1(0.)(qreg0), U2(0., 0.)(qreg0), U3(0., 0., 0.)(qreg0), Expii(0.)(qreg0), Expiz(0.)(qreg0), SH(qreg0), Swap(qreg0, qreg1), barrier(qreg0, qreg1) ] circuit += [ I.Adj(qreg0), H.Adj(qreg0), S.Adj(qreg0), T.Adj(qreg0), X.Adj(qreg0), Y.Adj(qreg0), Z.Adj(qreg0), Rx(0.).Adj(qreg0), Ry(0.).Adj(qreg0), Rz(0.).Adj(qreg0), U1(0.).Adj(qreg0), U2(0., 0.).Adj(qreg0), U3(0., 0., 0.).Adj(qreg0), Expii(0.).Adj(qreg0), Expiz(0.).Adj(qreg0), SH.Adj(qreg0), Swap(qreg0, qreg1), barrier(qreg0, qreg1) ] circuit += [ ctrl(qreg0).I(qreg1), ctrl(qreg0).H(qreg1), ctrl(qreg0).S(qreg1), ctrl(qreg0).T(qreg1), ctrl(qreg0).X(qreg1), ctrl(qreg0).Y(qreg1), ctrl(qreg0).Z(qreg1), ctrl(qreg0).Rx(0.)(qreg1), ctrl(qreg0).Ry(0.)(qreg1), ctrl(qreg0).Rz(0.)(qreg1), ctrl(qreg0).U1(0.)(qreg1), ctrl(qreg0).U2(0., 0.)(qreg1), ctrl(qreg0).U3(0., 0., 0.)(qreg1), ctrl(qreg0).Expii(0.)(qreg1), ctrl(qreg0).Expiz(0.)(qreg1), ctrl(qreg0).SH(qreg1) ] circuit += [ ctrl(qreg0).I.Adj(qreg1), ctrl(qreg0).H.Adj(qreg1), ctrl(qreg0).S.Adj(qreg1), ctrl(qreg0).T.Adj(qreg1), ctrl(qreg0).X.Adj(qreg1), ctrl(qreg0).Y.Adj(qreg1), ctrl(qreg0).Z.Adj(qreg1), ctrl(qreg0).Rx(0.).Adj(qreg1), ctrl(qreg0).Ry(0.).Adj(qreg1), ctrl(qreg0).Rz(0.).Adj(qreg1), ctrl(qreg0).U1(0.).Adj(qreg1), ctrl(qreg0).U2(0., 0.).Adj(qreg1), ctrl(qreg0).U3(0., 0., 0.).Adj(qreg1), ctrl(qreg0).Expii(0.).Adj(qreg1), ctrl(qreg0).Expiz(0.).Adj(qreg1), ctrl(qreg0).SH(qreg1) ] ref = new_reference() circuit += [measure(ref, qreg0)] qgate.dump(circuit, file=self.file)
# qregs for iqft. bits = new_qregs(n_bits) # initialize ops = [H(qreg) for qreg in bits] # set phase in the target qreg. # Ui = Expii(pi * theta * 2^i), U1, U2, U4, U8 ... for idx, ctrlreg in enumerate(bits): theta = 2 * math.pi * v_in * (1 << idx) ops.append(ctrl(ctrlreg).Expii(theta)(target)) # iqft gate sequence ops += iqft(bits) # dump circuit qgate.dump(ops) # run simulator sim = qgate.simulator.cpu() sim.run(ops) #sim.qubits.set_ordering(reversed(bits)) #qgate.dump(sim.qubits.prob) # creating sampling pool pool = sim.qubits.create_sampling_pool(bits) # sample 1024 times. obs = pool.sample(1024) # creating histgram hist = obs.histgram() #print(hist)