def make_circuit_ring(nq, depth, seed): assert int(nq / 2) == nq / 2 # for now size of ring must be even # Create a Quantum Register q = QuantumRegister(nq) # Create a Classical Register c = ClassicalRegister(nq) # Create a Quantum Circuit qc = QuantumCircuit(q, c) offset = 1 # initial round of random single-qubit unitaries for i in range(nq): qc.h(q[i]) for j in range(depth): for i in range(int(nq / 2)): # round of CNOTS k = i * 2 + offset + j % 2 # j%2 makes alternating rounds overlap qc.cx(q[k % nq], q[(k + 1) % nq]) for i in range(nq): # round of single-qubit unitaries if HAS_RANDOM_UNITARY: u = random_unitary(2, seed).data else: u = random_unitary_matrix(2) angles = euler_angles_1q(u) qc.u3(angles[0], angles[1], angles[2], q[i]) # insert the final measurements qcm = copy.deepcopy(qc) for i in range(nq): qcm.measure(q[i], c[i]) return [qc, qcm, nq]
def time_state_tomography_random(self, n_qubits): # random target state: first column of a random unitary target = random_unitary_matrix(pow(2, n_qubits))[0] if not self.use_quantum_program: self._state_tomography(target, 'random', n_qubits) else: self._state_tomography_quantum_program(target, 'random', n_qubits)
def test_kak_decomposition(self): """Verify KAK decomposition for random Haar unitaries. """ for _ in range(100): unitary = random_unitary_matrix(4) with self.subTest(unitary=unitary): try: two_qubit_kak(unitary, verify_gate_sequence=True) except MapperError as ex: self.fail(str(ex))
def quantum_volume(qubit, final_measure=True, depth=10, seed=0): qreg = QuantumRegister(qubit) width = qubit np.random.seed(seed) circuit = QuantumCircuit(qreg, name=f"Qvolume: {width} by {depth}, seed: {seed}") for _ in range(depth): # Generate uniformly random permutation Pj of [0...n-1] perm = np.random.permutation(width) # For each pair p in Pj, generate Haar random U(4) # Decompose each U(4) into CNOT + SU(2) for k in range(width // 2): U = random_unitary_matrix(4) for gate in two_qubit_kak(U): qs = [qreg[int(perm[2 * k + i])] for i in gate["args"]] pars = gate["params"] name = gate["name"] if name == "cx": circuit.cx(qs[0], qs[1]) elif name == "u1": circuit.u1(pars[0], qs[0]) elif name == "u2": circuit.u2(*pars[:2], qs[0]) elif name == "u3": circuit.u3(*pars[:3], qs[0]) elif name == "id": pass # do nothing else: raise Exception(f"Unexpected gate name: {name}") circuit.barrier(qreg) if final_measure: creg = ClassicalRegister(qubit) circuit.add_register(creg) circuit.measure(qreg, creg) return circuit
def build_model_circuit_kak(width, depth, seed=None): """Create quantum volume model circuit on quantum register qreg of given depth (default depth is equal to width) and random seed. The model circuits consist of layers of Haar random elements of U(4) applied between corresponding pairs of qubits in a random bipartition. """ qreg = QuantumRegister(width) depth = depth or width np.random.seed(seed) circuit = QuantumCircuit(qreg, name="Qvolume: %s by %s, seed: %s" % (width, depth, seed)) for _ in range(depth): # Generate uniformly random permutation Pj of [0...n-1] perm = np.random.permutation(width) # For each pair p in Pj, generate Haar random U(4) # Decompose each U(4) into CNOT + SU(2) for k in range(width // 2): U = random_unitary_matrix(4) for gate in two_qubit_kak(U): qs = [qreg[int(perm[2 * k + i])] for i in gate["args"]] pars = gate["params"] name = gate["name"] if name == "cx": circuit.cx(qs[0], qs[1]) elif name == "u1": circuit.u1(pars[0], qs[0]) elif name == "u2": circuit.u2(*pars[:2], qs[0]) elif name == "u3": circuit.u3(*pars[:3], qs[0]) elif name == "id": pass # do nothing else: raise Exception("Unexpected gate name: %s" % name) return circuit
def state_tomography(state, n_qubits, shots): # cat target state: [1. 0. 0. ... 0. 0. 1.]/sqrt(2.) if state == 'cat': target = np.zeros(pow(2, n_qubits)) target[0] = 1 target[pow(2, n_qubits)-1] = 1.0 target /= np.sqrt(2.0) # random target state: first column of a random unitary elif state == 'random': target = random_unitary_matrix(pow(2, n_qubits))[0] else: raise QISKitError("Unknown state for tomography.") print("target: {}".format(target)) # Use the local qasm simulator backend = 'local_qasm_simulator' qp = QuantumProgram() # Prepared target state and assess quality qp = target_prep(qp, state, target) prep_result = qp.execute(['prep'], backend='local_statevector_simulator') prep_state = prep_result.get_data('prep')['statevector'] F_prep = state_fidelity(prep_state, target) print('Prepared state fidelity =', F_prep) # Run state tomography simulation and fit data to reconstruct circuit qp, tomo_set, tomo_circuits = add_tomo_circuits(qp) tomo_result = qp.execute(tomo_circuits, backend=backend, shots=shots) tomo_data = tomo.tomography_data(tomo_result, 'prep', tomo_set) rho_fit = tomo.fit_tomography_data(tomo_data) # calculate fidelity and purity of fitted state F_fit = state_fidelity(rho_fit, target) pur = purity(rho_fit) print('Fitted state fidelity =', F_fit) print('Fitted state purity =', str(pur)) return qp