def test_probability_threshold2(self): """ p_zero=False w/ labels """ qc = qiskit.QuantumCircuit(1) counts = utility.get_counts(qc) pt = ProbabilityThreshold(0, p_zero=False, labels=['a', 'b']) result = pt.output(counts) self.assertTrue(result[0] == 'b')
def test_expectation1(self): """ Z """ qc = qiskit.QuantumCircuit(1) exp = Expectation(0) counts = utility.get_counts(qc) result = exp.output(counts) expected = [1] is_correct = (result == expected) qc1 = qiskit.QuantumCircuit(1) qc1.x(0) counts1 = utility.get_counts(qc1) result1 = exp.output(counts1) expected1 = [-1] is_correct1 = (result1 == expected1) self.assertTrue(is_correct and is_correct1)
def test_probability_threshold3(self): """ threshold """ qc = qiskit.QuantumCircuit(1) qc.h(0) counts = utility.get_counts(qc) pt = ProbabilityThreshold(0, threshold=0.75) result = pt.output(counts) self.assertTrue(result[0] == 1)
def test_probability5(self): """ Test multi-qubit measurment on a subset of measured qubits """ qc = qiskit.QuantumCircuit(3) prob = Probability([0, 2]) counts = utility.get_counts(qc, measure_all=True) result = prob.output(counts) expected = [1, 1] self.assertTrue(np.allclose(result, expected))
def test_add_measurements3(self): """ Test with clbit argument """ qc = qiskit.QuantumCircuit(2) qc.x(0) Measurement.add_measurements(qc, [0, 1], [1, 0]) counts = utility.get_counts(qc, measure_all=False) self.assertTrue(counts['10'] == 1024)
def test_add_measurements1(self): """ Test on circuit initially without classical register """ qc = qiskit.QuantumCircuit(1) qc.x(0) Measurement.add_measurements(qc, [0]) counts = utility.get_counts(qc, measure_all=False) self.assertTrue(counts['1'] == 1024)
def test_add_measurements4(self): """ Test with add_classical_register=False """ qc = qiskit.QuantumCircuit(2, 1) qc.x(0) Measurement.add_measurements(qc, [0], [0], add_classical_register=False) counts = utility.get_counts(qc, measure_all=False) self.assertTrue(counts['1'] == 1024)
def test_probability3(self): """ observable_basis. Note: test may fail probabilistically when there is no error. """ qc = qiskit.QuantumCircuit(1) X_obs = Observable.X() prob = Probability([0], observable=X_obs) prob.rotate_basis(qc) counts = utility.get_counts(qc) result = prob.output(counts) self.assertTrue(isclose(0.5, result[0], abs_tol=5e-2))
def test_expectation2(self): """ qubit """ qc = qiskit.QuantumCircuit(2) qc.h(0) exp = Expectation(1) exp.rotate_basis(qc) counts = utility.get_counts(qc) result = exp.output(counts) expected = [1] is_correct = (result == expected)
def test_add_measurements2(self): """ Test on circuit with an existing classical register. Should not overwrite measurements on existing classial registers. """ qc = qiskit.QuantumCircuit(2, 1) qc.x(1) qc.measure(0, 0) Measurement.add_measurements(qc, [1]) counts = utility.get_counts(qc, measure_all=False) self.assertTrue(counts['1 0'] == 1024) # space separates different classical registers
def test_probability4(self): """ Test multi-qubit measurment """ qc = qiskit.QuantumCircuit(3) prob = Probability([0, 2]) Measurement.add_measurements(qc, [0, 2]) counts = utility.get_counts(qc, measure_all=False) result = prob.output(counts) expected = [1, 1] self.assertTrue(np.allclose(result, expected))
def test_expectation2(self): """ Y """ qc = qiskit.QuantumCircuit(1) qc.rx(-np.pi/2, 0) Y_obs = Observable.Y() exp = Expectation(0, observable=Y_obs) exp.rotate_basis(qc) counts = utility.get_counts(qc) result = exp.output(counts) expected = [1] is_correct = (result == expected) qc1 = qiskit.QuantumCircuit(1) qc1.rx(np.pi/2, 0) exp.rotate_basis(qc1) counts1 = utility.get_counts(qc1) result1 = exp.output(counts1) expected1 = [-1] is_correct1 = (result1 == expected1) self.assertTrue(is_correct and is_correct1)
def test_probability1(self): """ Default arguments. Note: test may fail probabilistically when there is no error.""" qc = qiskit.QuantumCircuit(1) qc.h(0) Measurement.add_measurements(qc, [0]) counts = utility.get_counts(qc, measure_all=False) Prob = Probability(0) result = Prob.output(counts) self.assertTrue(isclose(0.5, result[0], abs_tol=5e-2))
def test_probability2(self): """ p_zero=False """ qc = qiskit.QuantumCircuit(1) qc.x(0) Measurement.add_measurements(qc, [0]) counts = utility.get_counts(qc, measure_all=False) Prob = Probability([0], p_zero=False) result = Prob.output(counts) self.assertTrue(1 == result[0])
def test_probability_threshold4(self): """ rotate_basis """ qc = qiskit.QuantumCircuit(1) qc.x(0) qc.h(0) X_obs = Observable.X() pt = ProbabilityThreshold(0, observable=X_obs) pt.rotate_basis(qc) counts = utility.get_counts(qc) result = pt.output(counts) self.assertTrue(result[0] == 1)
def test_counts_to_probability(self): qc = qiskit.QuantumCircuit(3) qc.x(0) qc.y(1) qc.z(2) counts = utility.get_counts(qc) meas_counts = Measurement.counts_to_probability(counts) correct_len = (len(counts) == len(meas_counts)) correct_type = isinstance(meas_counts, dict) self.assertTrue( (correct_len and correct_type) )
def test_counts_for_qubits(self): qc = qiskit.QuantumCircuit(3) qc.x(0) qc.y(1) qc.z(2) counts = utility.get_counts(qc) qubits = [1, 2] qubit_counts = Measurement.counts_for_qubits(counts, qubits) correct_len = (len(qubit_counts) == 2**(len(qubits))) correct_type = isinstance(qubit_counts, dict) self.assertTrue( (correct_len and correct_type) )
def test_basis_encoding2(self): """ Default """ x = [1, 0, 0, 0, 1, 1, 1] basis = encode.BasisEncoding() circuit = basis.circuit(x) counts = get_counts(circuit) key = None value = None for k, v in counts.items(): key = k expected_dirac_label = '1000111' self.assertEqual(len(counts.items()), 1) self.assertEqual(key, expected_dirac_label)
def test_probability_threshold1(self): qc = qiskit.QuantumCircuit(1) counts = utility.get_counts(qc) pt = ProbabilityThreshold(0) result = pt.output(counts) self.assertTrue(result[0] == 0)