def make_gate(n, index, pauli_id): """ゲートを作る関数""" circuit = QuantumCircuit(n) for i in range(len(index)): gate_number = index[i] if pauli_id[i] == 1: circuit.add_X_gate(gate_number) elif pauli_id[i] == 2: circuit.add_Y_gate(gate_number) elif pauli_id[i] == 3: circuit.add_Z_gate(gate_number) return circuit
def _try_append_gate(self, op: ops.GateOperation, qulacs_circuit: qulacs.QuantumCircuit, indices: np.array): # One qubit gate if isinstance(op.gate, ops.pauli_gates._PauliX): qulacs_circuit.add_X_gate(indices[0]) elif isinstance(op.gate, ops.pauli_gates._PauliY): qulacs_circuit.add_Y_gate(indices[0]) elif isinstance(op.gate, ops.pauli_gates._PauliZ): qulacs_circuit.add_Z_gate(indices[0]) elif isinstance(op.gate, ops.common_gates.HPowGate): qulacs_circuit.add_H_gate(indices[0]) elif isinstance(op.gate, ops.common_gates.XPowGate): qulacs_circuit.add_RX_gate(indices[0], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.common_gates.YPowGate): qulacs_circuit.add_RY_gate(indices[0], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.common_gates.ZPowGate): qulacs_circuit.add_RZ_gate(indices[0], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.SingleQubitMatrixGate): mat = op.gate._matrix qulacs_circuit.add_dense_matrix_gate(indices[0], mat) elif isinstance(op.gate, circuits.qasm_output.QasmUGate): lmda = op.gate.lmda theta = op.gate.theta phi = op.gate.phi gate = qulacs.gate.U3(indices[0], theta * np.pi, phi * np.pi, lmda * np.pi) qulacs_circuit.add_gate(gate) # Two qubit gate elif isinstance(op.gate, ops.common_gates.CNotPowGate): if op.gate._exponent == 1.0: qulacs_circuit.add_CNOT_gate(indices[0], indices[1]) else: mat = _get_google_rotx(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[1], mat) gate.add_control_qubit(indices[0], 1) qulacs_circuit.add_gate(gate) elif isinstance(op.gate, ops.common_gates.CZPowGate): if op.gate._exponent == 1.0: qulacs_circuit.add_CZ_gate(indices[0], indices[1]) else: mat = _get_google_rotz(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[1], mat) gate.add_control_qubit(indices[0], 1) qulacs_circuit.add_gate(gate) elif isinstance(op.gate, ops.common_gates.SwapPowGate): if op.gate._exponent == 1.0: qulacs_circuit.add_SWAP_gate(indices[0], indices[1]) else: qulacs_circuit.add_dense_matrix_gate(indices, op._unitary_()) elif isinstance(op.gate, ops.parity_gates.XXPowGate): qulacs_circuit.add_multi_Pauli_rotation_gate( indices, [1, 1], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.parity_gates.YYPowGate): qulacs_circuit.add_multi_Pauli_rotation_gate( indices, [2, 2], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.parity_gates.ZZPowGate): qulacs_circuit.add_multi_Pauli_rotation_gate( indices, [3, 3], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.TwoQubitMatrixGate): indices.reverse() mat = op.gate._matrix qulacs_circuit.add_dense_matrix_gate(indices, mat) # Three qubit gate """ # deprecated because these functions cause errors in gpu elif isinstance(op.gate, ops.three_qubit_gates.CCXPowGate): mat = _get_google_rotx(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[2], mat) gate.add_control_qubit(indices[0],1) gate.add_control_qubit(indices[1],1) qulacs_circuit.add_gate(gate) elif isinstance(op.gate, ops.three_qubit_gates.CCZPowGate): mat = _get_google_rotz(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[2], mat) gate.add_control_qubit(indices[0],1) gate.add_control_qubit(indices[1],1) qulacs_circuit.add_gate(gate) """ elif isinstance(op.gate, ops.three_qubit_gates.CSwapGate): mat = np.zeros(shape=(4, 4)) mat[0, 0] = 1 mat[1, 2] = 1 mat[2, 1] = 1 mat[3, 3] = 1 gate = qulacs.gate.DenseMatrix(indices[1:], mat) gate.add_control_qubit(indices[0], 1) qulacs_circuit.add_gate(gate) # Misc elif protocols.has_unitary(op): indices.reverse() mat = op._unitary_() qulacs_circuit.add_dense_matrix_gate(indices, mat) # Not unitary else: return False return True
def main(): ## Example1 circuit = QuantumCircuit(3) circuit.add_X_gate(0) circuit.add_Y_gate(1) circuit.add_Z_gate(2) circuit.add_dense_matrix_gate([0,1], [[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]]) circuit.add_CNOT_gate(2,0) circuit.add_X_gate(2) draw_circuit(circuit, verbose=1) ## Example2 circuit = QuantumCircuit(3) circuit.add_X_gate(0) circuit.add_Y_gate(1) circuit.add_dense_matrix_gate([0,1], [[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]]) circuit.add_Z_gate(2) circuit.add_CNOT_gate(2,0) circuit.add_X_gate(2) draw_circuit(circuit, verbose=1) ## Example3 circuit = QuantumCircuit(3) circuit.add_X_gate(1) circuit.add_CZ_gate(0,2) circuit.add_X_gate(1) draw_circuit(circuit) ## Example4 circuit = QuantumCircuit(4) ##CCX0,2, 3 cx_gate = CNOT(2,3) cx_mat_gate = to_matrix_gate(cx_gate) control_index = 0 control_with_value = 1 cx_mat_gate.add_control_qubit(control_index, control_with_value) circuit.add_gate(cx_mat_gate) ##CCX1,2, 3 ccx = TOFFOLI(1,2, 3) circuit.add_gate(ccx) ##CCX1,2, 0 ccx = TOFFOLI(1,2, 0) circuit.add_gate(ccx) ##CCX1,3, 0 ccx = TOFFOLI(1,3, 0) circuit.add_gate(ccx) ##CCX1,3, 2 ccx = TOFFOLI(1,3, 2) circuit.add_gate(ccx) ##SWAP0,1 circuit.add_SWAP_gate(0,1) ##SWAP0,2 circuit.add_SWAP_gate(0,2) ##SWAP1,3 circuit.add_SWAP_gate(1,3) draw_circuit(circuit, verbose=1) ## Example5 circuit = QuantumCircuit(5) ## 3-qubit gate applied to [0,1,2] mat = np.identity(2**3) circuit.add_dense_matrix_gate([0,1,2], mat) ## 3-qubit gate applied to [0,3,4], and [1] qubit is control-qubit c_dense_gate = DenseMatrix([0,3,4], mat) control_index = 1 control_with_value = 1 c_dense_gate.add_control_qubit(control_index, control_with_value) circuit.add_gate(c_dense_gate) ## 3-qubit gate applied to [0,2,4] circuit.add_dense_matrix_gate([0,2,4], mat) ## SWAP gate aplied to [1,3], and [2] qubit is control-qubit swp_gate = to_matrix_gate(SWAP(1,3)) control_index = 2 control_with_value = 1 swp_gate.add_control_qubit(control_index, control_with_value) circuit.add_gate(swp_gate) draw_circuit(circuit)