def test_pauli_evolution(self): """Test the generation of Pauli blocks.""" encoding = PauliFeatureMap() time = 1.4 with self.subTest(pauli_string='ZZ'): evo = QuantumCircuit(2) evo.cx(0, 1) evo.p(2 * time, 1) evo.cx(0, 1) pauli = encoding.pauli_evolution('ZZ', time) self.assertTrue(Operator(pauli).equiv(evo)) with self.subTest(pauli_string='XYZ'): evo = QuantumCircuit(3) # X on the most-significant, bottom qubit, Z on the top evo.h(2) evo.rx(np.pi / 2, 1) evo.cx(0, 1) evo.cx(1, 2) evo.p(2 * time, 2) evo.cx(1, 2) evo.cx(0, 1) evo.rx(-np.pi / 2, 1) evo.h(2) pauli = encoding.pauli_evolution('XYZ', time) self.assertTrue(Operator(pauli).equiv(evo)) with self.subTest(pauli_string='I'): evo = QuantumCircuit(1) pauli = encoding.pauli_evolution('I', time) self.assertTrue(Operator(pauli).equiv(evo))
def test_parameter_prefix(self): """Test the Parameter prefix""" encoding_pauli = PauliFeatureMap(feature_dimension=2, reps=2, paulis=["ZY"], parameter_prefix="p") encoding_z = ZFeatureMap(feature_dimension=2, reps=2, parameter_prefix="q") encoding_zz = ZZFeatureMap(feature_dimension=2, reps=2, parameter_prefix="r") x = ParameterVector("x", 2) y = Parameter("y") self.assertEqual( str(encoding_pauli.parameters), "ParameterView([ParameterVectorElement(p[0]), ParameterVectorElement(p[1])])", ) self.assertEqual( str(encoding_z.parameters), "ParameterView([ParameterVectorElement(q[0]), ParameterVectorElement(q[1])])", ) self.assertEqual( str(encoding_zz.parameters), "ParameterView([ParameterVectorElement(r[0]), ParameterVectorElement(r[1])])", ) encoding_pauli_param_x = encoding_pauli.assign_parameters(x) encoding_z_param_x = encoding_z.assign_parameters(x) encoding_zz_param_x = encoding_zz.assign_parameters(x) self.assertEqual( str(encoding_pauli_param_x.parameters), "ParameterView([ParameterVectorElement(x[0]), ParameterVectorElement(x[1])])", ) self.assertEqual( str(encoding_z_param_x.parameters), "ParameterView([ParameterVectorElement(x[0]), ParameterVectorElement(x[1])])", ) self.assertEqual( str(encoding_zz_param_x.parameters), "ParameterView([ParameterVectorElement(x[0]), ParameterVectorElement(x[1])])", ) encoding_pauli_param_y = encoding_pauli.assign_parameters({1, y}) encoding_z_param_y = encoding_z.assign_parameters({1, y}) encoding_zz_param_y = encoding_zz.assign_parameters({1, y}) self.assertEqual(str(encoding_pauli_param_y.parameters), "ParameterView([Parameter(y)])") self.assertEqual(str(encoding_z_param_y.parameters), "ParameterView([Parameter(y)])") self.assertEqual(str(encoding_zz_param_y.parameters), "ParameterView([Parameter(y)])")
def feature_map(): # BUILD FEATURE MAP HERE - START feature_dim = 3 # equal to the dimension of the data # from qiskit.circuit import QuantumCircuit, ParameterVector # num_qubits = 3 # reps = 1 # number of times you'd want to repeat the circuit # x = ParameterVector('x', length=num_qubits) # creating a list of Parameters # custom_circ = QuantumCircuit(num_qubits) # # defining our parametric form # for _ in range(reps): # for i in range(num_qubits): # custom_circ.rx(x[i], i) # for i in range(num_qubits): # for j in range(0, 1): # custom_circ.cx(i, j) # if i in {1,0} or j in {1,0}: # custom_circ.u1(x[i] * x[j], j) # custom_circ.cx(i, j) # import required qiskit libraries if additional libraries are required # build the feature map # feature_map = ZFeatureMap(feature_dimension=feature_dim, reps=5) feature_map = PauliFeatureMap(feature_dimension=feature_dim, entanglement='full', reps=1, paulis=['X', 'Y', 'XY']) # feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=5,entanglement='linear', insert_barriers=True) # BUILD FEATURE MAP HERE - END #return the feature map which is either a FeatureMap or QuantumCircuit object return feature_map
def feature_map(): # BUILD FEATURE MAP HERE - START # import required qiskit libraries if additional libraries are required def custom_map(x): coeff = 0 if len(x) == 1: coeff = x[0] elif len(x) == 3: coeff = functools.reduce(lambda l, m, n: 1 * m * n, np.pi - x) return coeff # build the feature map feature_map = None feature_map = PauliFeatureMap(feature_dimension=3, reps=5, entanglement="full", paulis=['Z', 'ZZ'], data_map_func=custom_map) # BUILD FEATURE MAP HERE - END #return the feature map which is either a FeatureMap or QuantumCircuit object return feature_map
def test_pauli_empty(self): """Test instantiating an empty Pauli expansion.""" encoding = PauliFeatureMap() with self.subTest(msg='equal to empty circuit'): self.assertTrue(Operator(encoding).equiv(QuantumCircuit())) with self.subTest(msg='rotation blocks is H gate'): self.assertEqual(len(encoding.rotation_blocks), 1) self.assertIsInstance(encoding.rotation_blocks[0].data[0][0], HGate)
def test_pauli_evolution(self): """Test the generation of Pauli blocks.""" encoding = PauliFeatureMap() time = 1.4 with self.subTest(pauli_string="ZZ"): evo = QuantumCircuit(2) evo.cx(0, 1) evo.p(2 * time, 1) evo.cx(0, 1) pauli = encoding.pauli_evolution("ZZ", time) self.assertTrue(Operator(pauli).equiv(evo)) with self.subTest(pauli_string="XYZ"): # q_0: ─────────────■────────────────────────■────────────── # ┌─────────┐┌─┴─┐ ┌─┴─┐┌──────────┐ # q_1: ┤ Rx(π/2) ├┤ X ├──■──────────────■──┤ X ├┤ Rx(-π/2) ├ # └──┬───┬──┘└───┘┌─┴─┐┌────────┐┌─┴─┐├───┤└──────────┘ # q_2: ───┤ H ├────────┤ X ├┤ P(2.8) ├┤ X ├┤ H ├──────────── # └───┘ └───┘└────────┘└───┘└───┘ evo = QuantumCircuit(3) # X on the most-significant, bottom qubit, Z on the top evo.h(2) evo.rx(np.pi / 2, 1) evo.cx(0, 1) evo.cx(1, 2) evo.p(2 * time, 2) evo.cx(1, 2) evo.cx(0, 1) evo.rx(-np.pi / 2, 1) evo.h(2) pauli = encoding.pauli_evolution("XYZ", time) self.assertTrue(Operator(pauli).equiv(evo)) with self.subTest(pauli_string="I"): evo = QuantumCircuit(1) pauli = encoding.pauli_evolution("I", time) self.assertTrue(Operator(pauli).equiv(evo))
def feature_map(): # BUILD FEATURE MAP HERE - START # import required qiskit libraries if additional libraries are required # build the feature map feature_map = PauliFeatureMap(3, reps=1, paulis = ['ZZ', 'Y', 'X']) # linear = full # #ZZFeatureMap(feature_dimension = 3, reps = 1, entanglement='full') # BUILD FEATURE MAP HERE - END #return the feature map which is either a FeatureMap or QuantumCircuit object return feature_map
def feature_map(): # BUILD FEATURE MAP HERE - START # import required qiskit libraries if additional libraries are required # build the feature map feature_map = PauliFeatureMap(feature_dimension=3, reps=2, insert_barriers=True, paulis=['Y', 'Z', 'ZZ', 'ZZZ']) # BUILD FEATURE MAP HERE - END #return the feature map which is either a FeatureMap or QuantumCircuit object return feature_map
def test_num_parameters(self, num_qubits, reps, pauli_strings): """Test the number of parameters equals the number of qubits, independent of reps.""" encoding = PauliFeatureMap(num_qubits, paulis=pauli_strings, reps=reps) self.assertEqual(encoding.num_parameters, num_qubits) self.assertEqual(encoding.num_parameters_settable, num_qubits)
def test_pauli_alpha(self): """Test Pauli rotation factor (getter, setter).""" encoding = PauliFeatureMap() self.assertEqual(encoding.alpha, 2.0) encoding.alpha = 1.4 self.assertEqual(encoding.alpha, 1.4)
return coeff feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2, data_map_func=custom_data_map_func) qsvm = QSVM(feature_map=feature_map, training_dataset=training_dataset, test_dataset=test_dataset) result = qsvm.run(quantum_instance) print("testing success ratio: ", result['testing_accuracy']) feature_map = PauliFeatureMap(feature_dimension=feature_dim, reps=2, paulis=['Z', 'Y', 'ZZ']) qsvm = QSVM(feature_map=feature_map, training_dataset=training_dataset, test_dataset=test_dataset) result = qsvm.run(quantum_instance) print("testing success ratio: ", result['testing_accuracy']) feature_dim = 3 sample_Total_b, training_dataset_b, test_dataset_b, class_labels = ad_hoc_data( training_size=20, test_size=10, n=feature_dim, gap=0.3, plot_data=False) feature_map = PauliFeatureMap(feature_dimension=feature_dim, reps=2,