def test_list_op_to_circuit(self): """Test if unitary ListOps transpile to circuit. """ # generate unitary matrices of dimension 2,4,8, seed is fixed np.random.seed(233423) u2 = unitary_group.rvs(2) u4 = unitary_group.rvs(4) u8 = unitary_group.rvs(8) # pauli matrices as numpy.arrays x = np.array([[0.0, 1.0], [1.0, 0.0]]) y = np.array([[0.0, -1.0j], [1.0j, 0.0]]) z = np.array([[1.0, 0.0], [0.0, -1.0]]) # create MatrixOp and CircuitOp out of matrices op2 = MatrixOp(u2) op4 = MatrixOp(u4) op8 = MatrixOp(u8) c2 = op2.to_circuit_op() # algorithm using only matrix operations on numpy.arrays xu4 = np.kron(x, u4) zc2 = np.kron(z, u2) zc2y = np.kron(zc2, y) matrix = np.matmul(xu4, zc2y) matrix = np.matmul(matrix, u8) matrix = np.kron(matrix, u2) operator = Operator(matrix) # same algorithm as above, but using PrimitiveOps list_op = ((X ^ op4) @ (Z ^ c2 ^ Y) @ op8) ^ op2 circuit = list_op.to_circuit() # verify that ListOp.to_circuit() outputs correct quantum circuit self.assertTrue(operator.equiv(circuit), "ListOp.to_circuit() outputs wrong circuit!")
def assertQFTIsCorrect(self, qft, num_qubits=None, inverse=False, add_swaps_at_end=False): """Assert that the QFT circuit produces the correct matrix. Can be provided with an explicit number of qubits, if None is provided the number of qubits is set to ``qft.num_qubits``. """ if add_swaps_at_end: circuit = QuantumCircuit(*qft.qregs) for i in range(circuit.num_qubits // 2): circuit.swap(i, circuit.num_qubits - i - 1) qft = qft + circuit simulated = Operator(qft) num_qubits = num_qubits or qft.num_qubits expected = np.empty((2 ** num_qubits, 2 ** num_qubits), dtype=complex) for i in range(2 ** num_qubits): i_qiskit = int(bin(i)[2:].zfill(num_qubits)[::-1], 2) for j in range(i, 2 ** num_qubits): entry = np.exp(2 * np.pi * 1j * i * j / 2 ** num_qubits) / 2 ** (num_qubits / 2) j_qiskit = int(bin(j)[2:].zfill(num_qubits)[::-1], 2) expected[i_qiskit, j_qiskit] = entry if i != j: expected[j_qiskit, i_qiskit] = entry if inverse: expected = np.conj(expected) expected = Operator(expected) self.assertTrue(expected.equiv(simulated))
def test_gate(self, gate_cls, num_params, basis_gates): """Test standard gate simulation.""" circuit = self.gate_circuit(gate_cls, num_params=num_params, rng=self.RNG) target = Operator(circuit) result = execute(circuit, self.SIMULATOR, basis_gates=basis_gates).result() self.assertSuccess(result) value = Operator(result.get_unitary(0)) self.assertTrue(target.equiv(value), msg='{}, basis_gates = {}'.format( gate_cls.__name__, basis_gates))
def test_can_construct_operator(self): """Test that we can construct an Operator from a circuit that contains a Clifford gate.""" cliff = self.create_cliff1() qc = QuantumCircuit(4) qc.append(cliff, [3, 1, 2]) # Create an operator from the decomposition of qc into gates op1 = Operator(qc.decompose()) # Create an operator from qc directly op2 = Operator(qc) # Check that the two operators are equal self.assertTrue(op1.equiv(op2))
def assertFourierCheckingIsCorrect(self, f_truth_table, g_truth_table, fc_circuit): """Assert that the Fourier Checking circuit produces the correct matrix.""" simulated = Operator(fc_circuit) num_qubits = int(np.log2(len(f_truth_table))) # create Hadamard matrix, re-use approach from TestMCMT h_i = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]]) h_tot = np.array([1]) for _ in range(num_qubits): h_tot = np.kron(h_tot, h_i) f_mat = np.diag(f_truth_table) g_mat = np.diag(g_truth_table) expected = np.linalg.multi_dot([h_tot, g_mat, h_tot, f_mat, h_tot]) expected = Operator(expected) self.assertTrue(expected.equiv(simulated))