def teleport(state, mres): q0, q1 = map(int, twoQ_basis[mres]) s0_name = twoQ_basis[mres] + '0' s1_name = twoQ_basis[mres] + '1' s0 = qt.bra(s0_name) s1 = qt.bra(s1_name) a = (s0 * state).tr() b = (s1 * state).tr() red_state = (a * qt.ket([0], 2) + b * qt.ket([1], 2)).unit() H = Gate('SNOT', targets=0) sqrtX = Gate('SQRTNOT', targets=0) qc = QubitCircuit(N=1) if q1 == 1: qc.add_gate(sqrtX) qc.add_gate(sqrtX) if q0 == 1: qc.add_gate(H) qc.add_gate(sqrtX) qc.add_gate(sqrtX) qc.add_gate(H) gates_sequence = qc.propagators() scheme = oper.gate_sequence_product(gates_sequence) return scheme * red_state
def test_circuit_to_qutip() -> None: q0, q1, q2 = 0, 1, 2 circ0 = qf.Circuit() circ0 += qf.I(q0) circ0 += qf.Ph(0.1, q0) circ0 += qf.X(q0) circ0 += qf.Y(q1) circ0 += qf.Z(q0) circ0 += qf.S(q1) circ0 += qf.T(q2) circ0 += qf.H(q0) circ0 += qf.H(q1) circ0 += qf.H(q2) circ0 += qf.CNot(q0, q1) circ0 += qf.CNot(q1, q0) circ0 += qf.Swap(q0, q1) circ0 += qf.ISwap(q0, q1) circ0 += qf.CCNot(q0, q1, q2) circ0 += qf.CSwap(q0, q1, q2) circ0 == qf.I(q0) circ0 += qf.Rx(0.1, q0) circ0 += qf.Ry(0.2, q1) circ0 += qf.Rz(0.3, q2) circ0 += qf.V(q0) circ0 += qf.H(q1) circ0 += qf.CY(q0, q1) circ0 += qf.CZ(q0, q1) circ0 += qf.CS(q1, q2) circ0 += qf.CT(q0, q1) circ0 += qf.SqrtSwap(q0, q1) circ0 += qf.SqrtISwap(q0, q1) circ0 += qf.CCNot(q0, q1, q2) circ0 += qf.CSwap(q0, q1, q2) circ0 += qf.CPhase(0.1, q1, q2) # Not yet supported # circ0 += qf.B(q1, q2) # circ0 += qf.Swap(q1, q2) ** 0.1 qbc = xqutip.circuit_to_qutip(circ0) U = gate_sequence_product(qbc.propagators()) gate0 = qf.Unitary(U.full(), qubits=[0, 1, 2]) assert qf.gates_close(gate0, circ0.asgate()) circ1 = xqutip.qutip_to_circuit(qbc) assert qf.gates_close(circ0.asgate(), circ1.asgate())
def testGateParams(a1, a2, a3, a4): QC = QubitCircuit(2) QC.add_gate("RX", targets=0, arg_value=a1) QC.add_gate("RY", targets=1, arg_value=a2) QC.add_gate("CNOT", targets=1, controls=0) QC.add_gate("RY", targets=0, arg_value=a3) QC.add_gate("RX", targets=1, arg_value=a4) U_list = QC.propagators() finalGate = gate_sequence_product(U_list) return calcGateFidelityN(finalGate, targetGate, 2)
def _swap_qubits(self, all_keys, keys): swap_circuit = QubitCircuit(N=len(all_keys)) for i, key in enumerate(keys): j = all_keys.index(key) if j != i: gate = Gate("SWAP", targets=[i, j]) swap_circuit.add_gate(gate) all_keys[i], all_keys[j] = all_keys[j], all_keys[i] swap_mat = gate_sequence_product(swap_circuit.propagators()).full() return all_keys, swap_mat
def testStateParams(a1, a2, a3, a4, a5, a6): QC = QubitCircuit(3) QC.add_gate("RX", targets=0, arg_value=a1) QC.add_gate("RX", targets=1, arg_value=a2) QC.add_gate("RX", targets=2, arg_value=a3) QC.add_gate("CNOT", targets=1, controls=0) QC.add_gate("CNOT", targets=2, controls=0) QC.add_gate("RX", targets=0, arg_value=a4) QC.add_gate("RX", targets=1, arg_value=a5) QC.add_gate("RX", targets=2, arg_value=a6) U_list = QC.propagators() finalGate = gate_sequence_product(U_list) finalState = finalGate * initialState return calcStateFidelity(targetState, finalState)
def get_unitary_matrix(self) -> "np.ndarray": """Method to get unitary matrix of circuit without measurement. Returns: np.ndarray: the matrix for the circuit operations. """ if self._cache is None: if len(self.gates) == 0: self._cache = np.identity(2**self.size) return self._cache qc = QubitCircuit(self.size) qc.user_gates = { "X": x_gate, "Y": y_gate, "Z": z_gate, "S": s_gate, "T": t_gate } for gate in self.gates: name, indices = gate if name == 'h': qc.add_gate('SNOT', indices[0]) elif name == 'x': qc.add_gate('X', indices[0]) elif name == 'y': qc.add_gate('Y', indices[0]) elif name == 'z': qc.add_gate('Z', indices[0]) elif name == 'cx': qc.add_gate('CNOT', controls=indices[0], targets=indices[1]) elif name == 'ccx': qc.add_gate('TOFFOLI', controls=indices[:2], targets=indices[2]) elif name == 'swap': qc.add_gate('SWAP', indices) elif name == 't': qc.add_gate('T', indices[0]) elif name == 's': qc.add_gate('S', indices[0]) else: raise NotImplementedError self._cache = gate_sequence_product(qc.propagators()).full() return self._cache return self._cache
def test_translate_to_qutip() -> None: circ0 = qf.Circuit() circ0 += qf.Can(0.1, 0.2, 0.3, 0, 1) qbc = xqutip.circuit_to_qutip(circ0, translate=True) U = gate_sequence_product(qbc.propagators()) gate0 = qf.Unitary(U.full(), qubits=[0, 1]) assert qf.gates_close(gate0, circ0.asgate()) with pytest.raises(ValueError): xqutip.circuit_to_qutip(circ0, translate=False) circ1 = qf.Circuit() circ1 += qf.Can(0.1, 0.2, 0.3, "a", "b") with pytest.raises(ValueError): xqutip.circuit_to_qutip(circ1, translate=True)
def teleport(state, mres): qc = QubitCircuit(N=1) if mres == 4: state = state[6][0][0] * basis(2, 0) + state[7][0][0] * basis(2, 1) elif mres == 2: state = state[4][0][0] * basis(2, 0) + state[5][0][0] * basis(2, 1) state = -1j * state qc.add_gate("RX", targets=0, arg_value=-math.pi) elif mres == 1: state = state[2][0][0] * basis(2, 0) + state[3][0][0] * basis(2, 1) state = 1j * state qc.add_gate("RZ", targets=0, arg_value=-math.pi) else: state = state[0][0][0] * basis(2, 0) + state[1][0][0] * basis(2, 1) state = -1 * state qc.add_gate("RX", targets=0, arg_value=-math.pi) qc.add_gate("RZ", targets=0, arg_value=-math.pi) U_list = qc.propagators() state = gate_sequence_product(U_list) * state return state.unit()
def evolute(state): CNOT01 = Gate('CNOT', targets=1, controls=0) CNOT12 = Gate('CNOT', targets=2, controls=1) CNOT02 = Gate('CNOT', targets=2, controls=0) H0 = Gate('SNOT', targets=0) sqrtX0 = Gate('SQRTNOT', targets=0) H2 = Gate('SNOT', targets=2) qc = QubitCircuit(N=3) # qc.add_gate(sqrtX0) # qc.add_gate(sqrtX0) qc.add_gate(CNOT01) qc.add_gate(H0) # qc.add_gate(CNOT12) # qc.add_gate(H2) # qc.add_gate(CNOT02) # qc.add_gate(H2) gates_sequence = qc.propagators() scheme = oper.gate_sequence_product(gates_sequence) return scheme * state
def evolute(state): qc = QubitCircuit(N=3) qc.add_gate("CNOT", controls=0, targets=1) qc.add_gate("SNOT", targets=0) U_list = qc.propagators() return gate_sequence_product(U_list) * state
spins.append(qt.basis(2, 0)) initial_state = tensor(spins) QC = QubitCircuit(3) QC.add_gate("RX", targets=0, arg_value=0.5) QC.add_gate("RX", targets=1, arg_value=0.1) QC.add_gate("RX", targets=2, arg_value=0.2223472) QC.add_gate("CNOT", targets=1, controls=0) QC.add_gate("CNOT", targets=2, controls=0) QC.add_gate("RX", targets=0, arg_value=0.26127) QC.add_gate("RX", targets=1, arg_value=1.3942948) QC.add_gate("RX", targets=1, arg_value=0.4378) U_list = QC.propagators() TargetGate = gate_sequence_product(U_list) class threeQubitCircuit(problem): def __init__(self, initialState_list=[initial_state], targetGate=TargetGate, configPath='./problems/three_qubits/three_q_config.yaml', verbose=2): targetState_list = [ targetGate * initialState for initialState in initialState_list ] testState_list = [ N3qubitStateFunc(initialState_list[i], targetState_list[i]) for i in range(len(initialState_list)) ]
def _gate_add(self, qc, command, custom_gates, classical_controls=None, control_value=None): ''' Add gates to :class:`.QubitCircuit` from processed tokens, define custom gates if necessary. Parameters ---------- qc: :class:`.QubitCircuit` circuit object to which gate is added command: list of str list of tokens corresponding to gate application custom_gates: {gate name : gate function or unitary} dictionary of user gates defined for qutip classical_controls : int or list of int, optional indices of classical bits to control gate on. control_value : int, optional value of classical bits to control on, the classical controls are interpreted as an integer with lowest bit being the first one. If not specified, then the value is interpreted to be 2 ** len(classical_controls) - 1 (i.e. all classical controls are 1). ''' args, regs = _gate_processor(command) reg_set = self._regs_processor(regs, "gate") if args: gate_name = "{}({})".format(command[0], ",".join(args)) else: gate_name = "{}".format(command[0]) # creates custom-gate (if required) using gate defn and provided args if (command[0] not in self.predefined_gates and command[0] not in custom_gates): n = len(reg_set[0]) qc_temp = QubitCircuit(n) self._custom_gate(qc_temp, [command[0], args, [str(i) for i in range(n)]]) unitary_mat = gate_sequence_product(qc_temp.propagators()) custom_gates[gate_name] = unitary_mat qc.user_gates = custom_gates # adds gate to the QubitCircuit for regs in reg_set: regs = [int(i) for i in regs] if command[0] in self.predefined_gates: args = [eval(arg) for arg in args] self._add_predefined_gates( qc, command[0], regs, args, classical_controls=classical_controls, control_value=control_value) else: if not isinstance(regs, list): regs = [regs] qc.add_gate(gate_name, targets=regs, classical_controls=classical_controls, control_value=control_value)