def test_circuit_add_gate(self): from qulacs import QuantumCircuit, QuantumState from qulacs.gate import Identity, X, Y, Z, H, S, Sdag, T, Tdag, sqrtX, sqrtXdag, sqrtY, sqrtYdag from qulacs.gate import P0, P1, U1, U2, U3, RX, RY, RZ, CNOT, CZ, SWAP, TOFFOLI, FREDKIN, Pauli, PauliRotation from qulacs.gate import DenseMatrix, SparseMatrix, DiagonalMatrix, RandomUnitary, ReversibleBoolean, StateReflection from qulacs.gate import BitFlipNoise, DephasingNoise, IndependentXZNoise, DepolarizingNoise, TwoQubitDepolarizingNoise, AmplitudeDampingNoise, Measurement from qulacs.gate import merge, add, to_matrix_gate, Probabilistic, CPTP, Instrument, Adaptive from scipy.sparse import lil_matrix qc = QuantumCircuit(3) qs = QuantumState(3) ref = QuantumState(3) sparse_mat = lil_matrix((4, 4)) sparse_mat[0, 0] = 1 sparse_mat[1, 1] = 1 def func(v, d): return (v + 1) % d def adap(v): return True gates = [ Identity(0), X(0), Y(0), Z(0), H(0), S(0), Sdag(0), T(0), Tdag(0), sqrtX(0), sqrtXdag(0), sqrtY(0), sqrtYdag(0), Probabilistic([0.5, 0.5], [X(0), Y(0)]), CPTP([P0(0), P1(0)]), Instrument([P0(0), P1(0)], 1), Adaptive(X(0), adap), CNOT(0, 1), CZ(0, 1), SWAP(0, 1), TOFFOLI(0, 1, 2), FREDKIN(0, 1, 2), Pauli([0, 1], [1, 2]), PauliRotation([0, 1], [1, 2], 0.1), DenseMatrix(0, np.eye(2)), DenseMatrix([0, 1], np.eye(4)), SparseMatrix([0, 1], sparse_mat), DiagonalMatrix([0, 1], np.ones(4)), RandomUnitary([0, 1]), ReversibleBoolean([0, 1], func), StateReflection(ref), BitFlipNoise(0, 0.1), DephasingNoise(0, 0.1), IndependentXZNoise(0, 0.1), DepolarizingNoise(0, 0.1), TwoQubitDepolarizingNoise(0, 1, 0.1), AmplitudeDampingNoise(0, 0.1), Measurement(0, 1), merge(X(0), Y(1)), add(X(0), Y(1)), to_matrix_gate(X(0)), P0(0), P1(0), U1(0, 0.), U2(0, 0., 0.), U3(0, 0., 0., 0.), RX(0, 0.), RY(0, 0.), RZ(0, 0.), ] gates.append(merge(gates[0], gates[1])) gates.append(add(gates[0], gates[1])) ref = None for gate in gates: qc.add_gate(gate) for gate in gates: qc.add_gate(gate) qc.update_quantum_state(qs) qc = None qs = None for gate in gates: gate = None gates = None parametric_gates = None
def qtest(angle): state = QuantumState(3) state.set_Haar_random_state() circuit = QuantumCircuit(3) circuit.add_X_gate(0) merged_gate = merge(CNOT(0, 1), Y(1)) circuit.add_gate(merged_gate) circuit.add_RX_gate(1, angle) circuit.update_quantum_state(state) observable = Observable(3) observable.add_operator(2.0, "X 2 Y 1 Z 0") observable.add_operator(-3.0, "Z 2") result = observable.get_expectation_value(state) output = {'energy': result} return (output)
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)
angle_z = np.arccos(x**2) for i in range(nqubit): U.add_RY_gate(i, angle_y) U.add_RZ_gate(i, angle_z) return U circuit = QuantumCircuit(nqubit) #circuit.add_H_gate(0) #circuit.add_H_gate(1) #circuit.add_H_gate(2) circuit.add_RX_gate(0, 0.1) circuit.add_RX_gate(1, 0.1) #circuit.add_RX_gate(2, 0.1) merged_gate = merge(CNOT(0,1),Y(1)) circuit.add_gate(merged_gate) meas0 = Measurement(0, 0) circuit.add_gate(meas0) meas1 = Measurement(1, 1) circuit.add_gate(meas1) #meas2 = Measurement(2, 2) #circuit.add_gate(meas2) print(circuit) observable = Observable(nqubit) observable.add_operator(1, "Z 0") observable.add_operator(2, "Z 1") #observable.add_operator(4, "Z 2") data = list()