def test_multi_qubits(self): """ Test for multi-qubits system. """ N = 3 H_d = tensor([sigmaz()] * 3) H_c = [] # test empty ctrls num_tslots = 30 evo_time = 10 test = OptPulseProcessor(N) test.add_drift(H_d, [0, 1, 2]) test.add_control(tensor([sigmax(), sigmax()]), cyclic_permutation=True) # test periodically adding ctrls sx = sigmax() iden = identity(2) # print(test.ctrls) # print(Qobj(tensor([sx, iden, sx]))) assert_(Qobj(tensor([sx, iden, sx])) in test.ctrls) assert_(Qobj(tensor([iden, sx, sx])) in test.ctrls) assert_(Qobj(tensor([sx, sx, iden])) in test.ctrls) test.add_control(sigmax(), cyclic_permutation=True) test.add_control(sigmay(), cyclic_permutation=True) # test pulse genration for cnot gate, with kwargs qc = [tensor([identity(2), cnot()])] test.load_circuit(qc, num_tslots=num_tslots, evo_time=evo_time, min_fid_err=1.0e-6) rho0 = qubit_states(3, [1, 1, 1]) rho1 = qubit_states(3, [1, 1, 0]) result = test.run_state(rho0, options=Options(store_states=True)) assert_(fidelity(result.states[-1], rho1) > 1 - 1.0e-6)
def test_custom_gates(): filename = "test_custom_gates.qasm" filepath = Path(__file__).parent / 'qasm_files' / filename qc = read_qasm(filepath) unitaries = qc.propagators() assert (unitaries[0] - unitaries[1]).norm() < 1e-12 ry_cx = cnot() * tensor(identity(2), ry(np.pi/2)) assert (unitaries[2] - ry_cx).norm() < 1e-12
def test_cnot_explicit(self): test = gates.expand_operator(gates.cnot(), 3, [2, 0]).full() expected = np.array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0]]) np.testing.assert_allclose(test, expected, atol=1e-15)
def test_EntanglingPower(): "Entropy: Entangling power" assert_(abs(entangling_power(cnot()) - 2 / 9) < 1e-12) assert_(abs(entangling_power(iswap()) - 2 / 9) < 1e-12) assert_(abs(entangling_power(berkeley()) - 2 / 9) < 1e-12) assert_(abs(entangling_power(sqrtswap()) - 1 / 6) < 1e-12) alpha = 2 * np.pi * np.random.rand() assert_( abs( entangling_power(swapalpha(alpha)) - 1 / 6 * np.sin(np.pi * alpha)**2) < 1e-12) assert_(abs(entangling_power(swap()) - 0) < 1e-12)
def test_qpt_cnot(): "quantum process tomography for cnot gate" U_psi = cnot() U_rho = spre(U_psi) * spost(U_psi.dag()) N = 2 op_basis = [[qeye(2), sigmax(), 1j * sigmay(), sigmaz()] for i in range(N)] # op_label = [["i", "x", "y", "z"] for i in range(N)] chi1 = qpt(U_rho, op_basis) chi2 = np.zeros((2**(2 * N), 2**(2 * N)), dtype=complex) chi2[0, 0] = chi2[0, 1] = chi2[1, 0] = chi2[1, 1] = 0.25 chi2[12, 0] = chi2[12, 1] = 0.25 chi2[13, 0] = chi2[13, 1] = -0.25 chi2[0, 12] = chi2[1, 12] = 0.25 chi2[0, 13] = chi2[1, 13] = -0.25 chi2[12, 12] = chi2[13, 13] = 0.25 chi2[13, 12] = chi2[12, 13] = -0.25 assert_(la.norm(chi2 - chi1) < 1e-8)
def test_expand_operator(self): """ gate : expand qubits operator to a N qubits system. """ # oper size is N, no expansion oper = tensor(sigmax(), sigmay()) assert_allclose(expand_operator(oper=oper, targets=[0, 1], N=2), oper) assert_allclose(expand_operator(oper=oper, targets=[1, 0], N=2), tensor(sigmay(), sigmax())) # random single qubit gate test, integer as target r = rand_unitary(2) assert_allclose(expand_operator(r, 3, 0), tensor([r, identity(2), identity(2)])) assert_allclose(expand_operator(r, 3, 1), tensor([identity(2), r, identity(2)])) assert_allclose(expand_operator(r, 3, 2), tensor([identity(2), identity(2), r])) # random 2qubits gate test, list as target r2 = rand_unitary(4) r2.dims = [[2, 2], [2, 2]] assert_allclose(expand_operator(r2, 3, [2, 1]), tensor([identity(2), r2.permute([1, 0])])) assert_allclose(expand_operator(r2, 3, [0, 1]), tensor([r2, identity(2)])) assert_allclose(expand_operator(r2, 3, [0, 2]), tensor([r2, identity(2)]).permute([0, 2, 1])) # cnot expantion, qubit 2 control qubit 0 assert_allclose( expand_operator(cnot(), 3, [2, 0]), Qobj([[1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 1.], [0., 0., 0., 0., 1., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 1., 0., 0., 0., 0.]], dims=[[2, 2, 2], [2, 2, 2]])) # test expansion with cyclic permutation result = expand_operator(tensor([sigmaz(), sigmax()]), N=3, targets=[2, 0], cyclic_permutation=True) mat1 = tensor(sigmax(), qeye(2), sigmaz()) mat2 = tensor(sigmaz(), sigmax(), qeye(2)) mat3 = tensor(qeye(2), sigmaz(), sigmax()) assert_(mat1 in result) assert_(mat2 in result) assert_(mat3 in result) # test for dimensions other than 2 mat3 = rand_dm(3, density=1.) oper = tensor([sigmax(), mat3]) N = 4 dims = [2, 2, 3, 4] result = expand_operator(oper, N, targets=[0, 2], dims=dims) assert_array_equal(result.dims[0], dims) dims = [3, 2, 4, 2] result = expand_operator(oper, N, targets=[3, 0], dims=dims) assert_array_equal(result.dims[0], dims) dims = [3, 2, 4, 2] result = expand_operator(oper, N, targets=[1, 0], dims=dims) assert_array_equal(result.dims[0], dims)